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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:06:32+00:00 2026-05-17T01:06:32+00:00

Apart from one example in the docs, I can’t find any documentation on how

  • 0

Apart from one example in the docs, I can’t find any documentation on how exactly django chooses the name with which one can access the child object from the parent object. In their example, they do the following:

    class Place(models.Model):
        name = models.CharField(max_length=50)
        address = models.CharField(max_length=80)

        def __unicode__(self):
            return u"%s the place" % self.name

    class Restaurant(models.Model):
        place = models.OneToOneField(Place, primary_key=True)
        serves_hot_dogs = models.BooleanField()
        serves_pizza = models.BooleanField()

        def __unicode__(self):
            return u"%s the restaurant" % self.place.name

    # Create a couple of Places.
    >>> p1 = Place(name='Demon Dogs', address='944 W. Fullerton')
    >>> p1.save()
    >>> p2 = Place(name='Ace Hardware', address='1013 N. Ashland')
    >>> p2.save()

    # Create a Restaurant. Pass the ID of the "parent" object as this object's ID.
    >>> r = Restaurant(place=p1, serves_hot_dogs=True, serves_pizza=False)
    >>> r.save()

    # A Restaurant can access its place.
    >>> r.place
    <Place: Demon Dogs the place>
    # A Place can access its restaurant, if available.
    >>> p1.restaurant

So in their example, they simply call p1.restaurant without explicitly defining that name. Django assumes the name starts with lowercase. What happens if the object name has more than one word, like FancyRestaurant?

Side note: I’m trying to extend the User object in this way. Might that be the problem?

  • 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-17T01:06:33+00:00Added an answer on May 17, 2026 at 1:06 am

    If you define a custom related_name then it will use that, otherwise it will lowercase the entire model name (in your example .fancyrestaurant). See the else block in django.db.models.related code:

    def get_accessor_name(self):
        # This method encapsulates the logic that decides what name to give an
        # accessor descriptor that retrieves related many-to-one or
        # many-to-many objects. It uses the lower-cased object_name + "_set",
        # but this can be overridden with the "related_name" option.
        if self.field.rel.multiple:
            # If this is a symmetrical m2m relation on self, there is no reverse accessor.
            if getattr(self.field.rel, 'symmetrical', False) and self.model == self.parent_model:
                return None
            return self.field.rel.related_name or (self.opts.object_name.lower() + '_set')
        else:
            return self.field.rel.related_name or (self.opts.object_name.lower())
    

    And here’s how the OneToOneField calls it:

    class OneToOneField(ForeignKey):
        ... snip ...
    
        def contribute_to_related_class(self, cls, related):
            setattr(cls, related.get_accessor_name(),
                    SingleRelatedObjectDescriptor(related))
    

    The opts.object_name (referenced in the django.db.models.related.get_accessor_name) defaults to cls.__name__.

    As for

    Side note: I’m trying to extend the
    User object in this way. Might that be
    the problem?

    No it won’t, the User model is just a regular django model. Just watch out for related_name collisions.

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

Sidebar

Related Questions

I've used this code https://github.com/johannilsson/android-pulltorefresh It all works perfectly apart from one thing, when
Apart from Sql server profiler ,is there any third party user friendly tool available
Apart from jQuery, which other JavaScript libraries support Visual Studio2005/2008 intellisense integration?
Apart from using image on the button, how can I write X Y on
Is there any cost/drawback (apart from typing too much) to adorning a class with
Recently, I have been writing many classes which have, apart from generic variant, some
I am almost finished my recent project apart from one issue. I have a
Does anybody know any reasons why IMemAllocator:GetBuffer (Directshow) hangs, apart from all samples being
Apart from what is shown in the NXP websites regarding mifare plus and mifare
Apart from lxml , is anyone aware of Python packages that depend on libxml2

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.