← Back to Django Course | Chapter 6: Advanced ORM Queries | Lesson 6 of 9

One-to-Many संबंध

एक one-to-many relationship एक parent के कई children होने जैसा है — एक Chapter की कई Tutorials हो सकती हैं, लेकिन हर Tutorial का सिर्फ एक Chapter होता है।
Syntax
markup
class Child(models.Model):
    parent = models.ForeignKey(Parent, on_delete=models.CASCADE, related_name='children')

parent.children.all()   # all children of one parent

One-to-Many को Model करना

One-to-many का मतलब है एक side की एक row दूसरी side की कई rows से relate हो सकती है — many model पर एक ForeignKey लगाकर implement किया जाता है।

उदाहरण: Modeling One-to-Many

One Chapter can be the parent of many Tutorial rows, each pointing back to it through the chapter field.

markup
from django.db import models

class Chapter(models.Model):
    title = models.CharField(max_length=200)

class Tutorial(models.Model):
    chapter = models.ForeignKey(
        Chapter, related_name='tutorials', on_delete=models.CASCADE
    )
    title = models.CharField(max_length=200)
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Parent से Children Lookup करना

related_name आपको one side से उसकी सभी related many rows तक वापस जाने का एक friendly तरीका देता है।

Note: related_name के बिना, Django modelname_set पर default करता है, जैसे chapter.tutorial_set.all()।

उदाहरण: Looking Up Children from the Parent

Because related_name=tutorials was set on the ForeignKey, chapter.tutorials.all() returns every Tutorial linked to this chapter.

markup
from core.models import Chapter

chapter = Chapter.objects.first()
chapter.tutorials.all()
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

on_delete Behavior

on_delete Django को बताता है कि parent delete होने पर child rows के साथ क्या करना है — CASCADE उन्हें भी delete करता है, SET_NULL इसके बजाय link clear करता है।

Warning: Default रूप से CASCADE चुनना silently इरादे से ज़्यादा data delete कर सकता है।

उदाहरण: on_delete Behavior

If the related Chapter is deleted, this Tutorial's chapter field becomes null instead of the Tutorial itself being deleted.

markup
from django.db import models

class Tutorial(models.Model):
    chapter = models.ForeignKey(
        'Chapter', null=True, on_delete=models.SET_NULL
    )
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह सोचना कि many side को एक ManyToManyField चाहिए बजाय एक plain ForeignKey के — many model पर एक ForeignKey काफी है।
  2. reverse accessor name (default modelname_set है, जैसे chapter.tutorial_set) या set किया गया custom related_name भूल जाना।
  3. एक appropriate on_delete behavior न चुनना, गलती से CASCADE छोड़ देना जब SET_NULL इरादा था।
चैप्टर सारांश
  • One-to-many many side पर एक single ForeignKey से model होता है।
  • one side अपनी सभी related rows reverse relation से lookup कर सकता है, जैसे chapter.tutorials.all()।
  • on_delete control करता है कि parent delete होने पर child rows के साथ क्या होता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.