ForeignKey से Related Objects
In this page:
class Child(models.Model):
parent = models.ForeignKey(Parent, on_delete=models.CASCADE)
child.parent # the related Parent object
child.parent.field
एक ForeignKey क्या Represent करता है
एक ForeignKey field कहता है 'यह row exactly एक दूसरे model की row से belong करता है' — जैसे हर Tutorial एक Chapter से belong करती है।
उदाहरण: What a ForeignKey Represents
Every Tutorial row stores a reference to exactly one Chapter row through its 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, 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. #}
Related Object Access करना
एक बार ForeignKey set हो जाए, आप सिर्फ इसकी id नहीं बल्कि पूरा related object पाने के लिए simple dot notation से इसे follow कर सकते हैं।
उदाहरण: Accessing the Related Object
tutorial.chapter fetches the related Chapter object, and .title reads its title field.
from core.models import Tutorial
tutorial = Tutorial.objects.first()
print(tutorial.chapter.title)
{# 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 Field से Query करना
आप relationship name और field name को double underscores से chain करके एक related model की fields पर filter कर सकते हैं।
उदाहरण: Querying by a Related Field
This crosses the ForeignKey to filter tutorials based on a field of their related Chapter, all in one query.
from core.models import Tutorial
# Tutorials whose chapter title contains 'Forms'
Tutorial.objects.filter(chapter__title__icontains='Forms')
{# 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. #}
- यह भूल जाना कि एक ForeignKey के related object को access करना (जैसे tutorial.chapter) हर बार एक नई database query trigger करता है जब तक select_related() उपयोग न हो।
- ForeignKey attribute को एक model instance या _id suffix उपयोग करने के बजाय एक raw id (एक integer) assign करने की कोशिश करना।
- वह case handle न करना जहां एक ForeignKey null=True हो और related object None हो सकता हो।
- एक ForeignKey field एक model से दूसरे model की row तक एक link store करता है।
- Related object access करना simple dot notation जितना आसान है, जैसे tutorial.chapter।
- Django हर ForeignKey के लिए database में एक matching _id column automatically बनाता है।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: