'How to get list of all objects associated with a foreign key in Django

Not super experienced with Django so I apologize if this is trivial. Say I had a category instance and I wanted to get access to all of the content objects that I have previously added to my foreign key. How would I do so?

class Organization(models.Model):
    id = models.CharField(primary_key=True, max_length=200, default=uuid4, editable=False, unique=True)
    name=models.CharField(max_length=200,unique=True)
    phoneNumber=models.CharField(max_length=20)
    logo=models.CharField(max_length=100000) # storing this as base 64 encoded
    location=models.CharField(max_length=200)

class Category(models.Model):
    categoryName=models.CharField(max_length=300, unique=True, primary_key=True)
    associatedOrganizations=models.ForeignKey(Organization,on_delete=models.CASCADE,related_name='associatedOrgs',null=True,blank=True)
    associatedContent=models.ForeignKey(Content, on_delete=models.CASCADE,related_name='associatedContent',null=True,blank=True)
    associatedMeetingNotices=models.ForeignKey(MeetingNotice,on_delete=models.CASCADE,related_name='associatedMeetingNotices',null=True,blank=True)

For example: say I had the following

healthcareCategory=models.Category.objects.get(pk="healthcare")

and I wanted to access all Organizations related to healthcare, what should I do?

This?

healthcareCategory.associatedOrganizations.all()


Solution 1:[1]

You are close. You may want to change that related name from associatedOrgs to be associatedorgs to follow more closely to the django coding style. Documentation on this has a few examples.

healthcare_category = Category.objects.get(pk="healthcare")
organizations = healthcare_category.associatedorgs.all()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 AMG