2

I have the following models below:

class Account(models.Model):
    courses = models.ManyToManyField(Course)

class Course(models.Model):
    name = models.CharField(blank=True, null=True)

Can I access the account object pointing to a course from a course? Say for example:

course = Course.objects.get(id=1)

account_parent = course.parent_pointing_to_me
Jude Maranga
  • 865
  • 2
  • 9
  • 27

1 Answers1

3

You can get list of accout related to the class using account_set attribute (see docs):

account_parents = course.account_set.all()

Since account_set is related manager, you can use any other queryset method, for example if you need first account in the list:

account_parent = course.account_set.first()
neverwalkaloner
  • 46,181
  • 7
  • 92
  • 100