Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5928247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:16:13+00:00 2026-05-22T14:16:13+00:00

i have the following models class SchoolClass(models.Model): id = models.AutoField(primary_key = True) class_name =

  • 0

i have the following models

class SchoolClass(models.Model):
    id = models.AutoField(primary_key = True)
    class_name = models.TextField()
    level = models.IntegerField()
    taught_by = models.ManyToManyField(User,related_name="teacher_teaching",through='TeachSubject')
    attended_by = models.ManyToManyField(User,related_name='student_attending')

    def __unicode__(self):
        return self.class_name
    class Meta:
        db_table = 'classes'

class Relationship(models.Model):
    rChoices = (
        (1,'Mother'),
        (2,'Father'),
        (3,'Guardian'),
    )

    parent = models.ForeignKey(User,related_name='parent')
    student = models.ForeignKey(User,related_name='child')
    relationship = models.IntegerField(choices= rChoices)
    #add in __unicode__ for admin name

    class Meta:
        unique_together = ('parent','student')
        db_table = 'relationship

I have the the pk of the class, and I want to find out who are the parents of the students in the selected class.

My feeble attempt is:

selected_class =  SchoolClass.objects.get(pk=class_id)
studs = selected_class.attended_by.all().select_related()
r = Relationship.objects.filter(student__in=students)

parents = [.parent for p in r]

Now, I am just curious if there is a shorter or more efficient way of doing this(i’m sure missed something in the docs) ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T14:16:14+00:00Added an answer on May 22, 2026 at 2:16 pm

    This should work

    parents = Relationship.objects.filter(student__schoolclass__id=class_id).values_list('parent', flat=True)
    

    “To refer to a “reverse” relationship, just use the lowercase name of the model”. (docs)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following tables: class SchoolClass(models.Model): id = models.AutoField(primary_key = True) class_name =
I have the following models: class Fixture(models.Model): id = models.AutoField(primary_key=True) home_team = models.ForeignKey(Player, related_name=home)
I have the following models: class Foo(models.Model): field1 = models.IntegerField() ... class Bar(models.Model): field1
I have the following models: class Application(models.Model): users = models.ManyToManyField(User, through='Permission') folder = models.ForeignKey(Folder)
I have the following two models: class Provider(models.Model): provider = models.CharField(max_length=100, unique=True) class UserProfile(models.Model):
I have the following models: class Territory(models.Model): name = models.CharField(max_length=30) power = models.ForeignKey(Power, null=True,
I have following models class Artist(models.Model): name = models.CharField(max_length=200, unique=True) photo = models.CharField(max_length=250) views
I have the following models: class Topping(models.Model): ... class Pizza(models.Model): toppings = models.ManyToManyField(Topping) I
I have the following models -- class UserProfile(models.Model): user = models.ForeignKey(User, unique=True) ... class
I have the following models: class Product(models.Model): active = models.BooleanField(default=True) name = models.CharField(max_length=40, unique=True)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.