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 8427269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:44:26+00:00 2026-06-10T04:44:26+00:00

I have two models that are related together using ForeignKey and related_name is used.

  • 0

I have two models that are related together using ForeignKey and related_name is used. Here is an example.

class Student(models.Model):
    name = models.CharField(max_length=255)
    birthday = models.DateField(blank=True)


class Class(models.Model):

    name = models.CharField(max_length=255)
    student = models.ForeignKey(Student,
                                related_name='classes',
                                null=True)

    def __unicode__(self):
        return self.name

For example, I would like to access the class name.

This is what i tried.

john = Student.objects.get(username = 'john')
print john.classes.name

nothing’s get printed.

But when i try john.classes

i get django.db.models.fields.related.RelatedManager object at 0x109911410. This is shows that they are related. But i would like to get the class name.

Am i doing something wrong? How do i access the name of the class using related_name? Need some guidance.

  • 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-06-10T04:44:28+00:00Added an answer on June 10, 2026 at 4:44 am

    Yes, classes is a manager. It can be several classes for one teacher. So to output their names you should do:

    john = Student.objects.get(username='john')
    for class2 in john.classes.all():
       print class2.name
    

    If you want only one class for one student then use one-to-one relation. In this case you can access the related field with your method.

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

Sidebar

Related Questions

Say I have two models, Article and Category: class Article(models.Model): category = models.ForeignKey(Category, related_name='articles')
I have two models: class Foo(models.Model): foo_field = ... class Bar(models.Model): foo = models.ForeignKey(Foo,
I have two models that are related to each other. For a contrived example,
I have two models: class Product(models.Model): name = models.CharField(max_length=255) class ProductPhoto(models.Model): product = models.ForeignKey('Product',
I have two models related by a foreign key: # models.py class TestSource(models.Model): name
I have two models -- User and Entry -- that are related through a
I have a two models that are similar, but not exactly the same. Here's
Let's assume I have two gmdistibution models that i obtained using modeldata1=gmdistribution.fit(data1,1); modeldata2=gmdistribution.fit(data2,1); Now
Given these two models: class Profile(models.Model): user = models.ForeignKey(User, unique=True, verbose_name=_('user')) about = models.TextField(_('about'),
I have two models that are not related (in the db sense) but have

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.