One-to-Many संबंध
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.
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 तरीका देता है।
उदाहरण: 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.
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 करता है।
उदाहरण: on_delete Behavior
If the related Chapter is deleted, this Tutorial's chapter field becomes null instead of the Tutorial itself being deleted.
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. #}
- यह सोचना कि many side को एक ManyToManyField चाहिए बजाय एक plain ForeignKey के — many model पर एक ForeignKey काफी है।
- reverse accessor name (default modelname_set है, जैसे chapter.tutorial_set) या set किया गया custom related_name भूल जाना।
- एक 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 के साथ क्या होता है।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: