Home > Software design >  How to get list of all objects associated with a foreign key in Django
How to get list of all objects associated with a foreign key in Django

Time:02-20

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()

CodePudding user response:

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()

CodePudding user response:

The answer by @AMG is correct. If you had not defined a related_name for associatedOrganizations you could simply do

organizations = Organization.objects.filter(category__pk='healthcare')

But I think there is another issue. Am I correct in saying that an Organization can have only one Category, but a Category can have many Organizations?

If so, then I think the ForeignKey in your model is in the wrong place.

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)

    # The ForeignKey should be here:
    category = ForeignKey(Category, on_delete=models.CASCADE)


class Category(models.Model):
    categoryName=models.CharField(max_length=300, unique=True, primary_key=True)
    
    # remove this
    # associatedOrganizations=models.ForeignKey(Organization,on_delete=models.CASCADE,related_name='associatedOrgs',null=True,blank=True)

    ...

The ForeignKey is a ManyToOneField so you place it in the model that will be the many, and you link it to the model that will be the one.

Now you can find all organizations within the healthcare category like this:

organizations = Organization.objects.filter(category='healthcare')
  • Related