• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

OneToOneField in Django Model | related_name Example

Aniruddha Chaudhari/828/0
Django

In this tutorial, I’m explaining the OneToOneField relationship in the Django model and how to access these model fields in the Django template using related_name.

Django supports both OneToOneField and ForeignKey fields. Both are different concepts. For the foreign key, follow this tutorial.

Let’s get into the practical example where we are setting up the OneToOneField relationship in Django model.

OneToOneField Relationship in Django Model

Suppose you have this model called Author.

class Author(models.Model):
    author_name = models.CharField(max_length=120)

Now, you want to create another model which will have a one-to-one relation with the model ‘Author’, says ‘Contact’.

Use the ‘OneToOneField‘ model attribute as below.

class Contact(models.Model):
 	author = models.OneToOneField(User, on_delete=models.CASCADE, related_name="author_contact")
    address = models.CharField(max_length=120)
    mobile = models.IntegerField(blank=True, null=True)

Look at the above code, we are using related_name. This can be considered as one of the Django related name examples.

Note: If you are making any changes to the models.py, make sure you do the complete migrations.

Accessing OneToOneField Model fields in Django Templates

Now we are reading all the contacts and also reading the name of the author for each contact.

Get all the objects of the model ‘Contact’ in your views.py file.

contact = Contact.objects.filter()

And pass this to the template.

Now, go to your HTML template and add this code.

{% conatct for contacts %}
	{{conatct.author.author_name}}
  	{{conatct.address}}
  	{{conatct.mobile}}
{% endfor %}

This will show the list of all the contacts including the author’s name.

It’s simple. Isn’t it?

Now, what if you want to get the user contact details for each user?

Reverse Look Up (aka Backward Compatibility)

Python also supports backward compatibility. It is also called reverse compatibility.

Get all the objects of the model ‘Author’ in your views.py file.

authors = Author.objects.filter()

Here are using a related_name ‘author_contact’ that we set in the ‘Contact’ Django model.

{% auth for authors %}
	{{auth.author_name}}
  	{{auth.author_contact.address}}
  	{{auth.author_contact.mobile}}
{% endfor %}

Look, guys. I tried to keep this tutorial easy to refer. Hope you find it useful. If you have any questions, let me know in the comment below. Thanks!

Python Interview Questions eBook

Django
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack.org portal.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Leave a Reply Cancel reply

Prerequisite for Django

Learn Python Basics

Django Tutorials

Why Django?

Django- Create Your First Project

Django- Adding CSS/Static Files

Django- Use/Load Static Files

Django- Create Form

Django- Create Input-Tags

Django- Display Message on Form Submit

Django- MultipleChoiceField in Form

Django- Read Model Data (order_by)

Django- Read First/Last Queryset Object

Django- Update Model Field

Django- Signals

Django- Create TextField in Form

Django- Add Model to Admin Site

Django- Customize Admin Site

Django- Understanding Model User

Django- Registration/Signup Form

Django- Form Layout with Widget-Tweaks

Django- OneToOneField in Model

Django- Foreign Key & Reverse Lookup

Django- Custom Template Filters

Django- Interactive Shell

Django- Social Login

Django- Deploy and Host [FREE] 🔥

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard