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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:58:31+00:00 2026-06-01T06:58:31+00:00

What is different between models.ForeignKey(Modelname, unique=True) and models.OneToOneField in Django? Where should I use

  • 0

What is different between models.ForeignKey(Modelname, unique=True) and models.OneToOneField in Django?

Where should I use models.OneToOneField and models.ForeignKey(Modelname, unique=True)?

  • 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-01T06:58:32+00:00Added an answer on June 1, 2026 at 6:58 am

    A OneToOneField is very similar to a ForeignKey with unique=True. Unless you are doing multiple table inheritance, in which case you have to use OneToOneField, the only real difference is the api for accessing related objects.

    In the Django docs it says:

    Conceptually, this is similar to a ForeignKey with unique=True, but the “reverse” side of the relation will directly return a single object.

    Let’s show what that means with an example. Consider two models, Person and Address. We’ll assume each person has a unique address.

    class Person(models.Model):
        name = models.CharField(max_length=50)
        address = models.ForeignKey('Address', unique=True)
    
    class Address(models.Model):
        street = models.CharField(max_length=50)
    

    If you start with a person, you can access the address easily:

    address = person.address
    

    However if you start with an address, you have to go via the person_set manager to get the person.

    person = address.person_set.get() # may raise Person.DoesNotExist
    

    Now let’s replace the ForeignKey with a OneToOneField.

    class Person(models.Model):
        name = models.CharField(max_length=50)
        address = models.OneToOneField('Address')
    
    class Address(models.Model):
        street = models.CharField(max_length=50)
    

    If you start with a person, you can access the address in the same way:

    address = person.address
    

    And now, we can access the person from the address more easily.

    person = address.person # may raise Person.DoesNotExist
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to use different choices for subclasses of models? The following code
I had a many-to-many relationship between two Django Models: from django.contrib.auth.models import User class
I am sharing objects between different sites using the Django-sites framework. This works fine
When we add a model field in Django we generally write: models.CharField(max_length=100, null=True, blank=True)
What is actually the different between those start processes ? I know the simple
Is there any performance different between hosting your asp.net in mono on linux and
What is the different between clearfix hack and overflow:hidden vs overflow:auto ? Are all
What's the different between var myObject : Sprite = new Sprite(); and var myObject
What are the different between this two diff statement? Any reference document? '{<table width=100%
Is there a GUI tool using which I can design new Django models graphically?

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.