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

ForeignKey से Related Objects

एक ForeignKey दो toys के बीच बंधी एक string जैसा है, इसलिए एक toy हमेशा जानता है कि यह किस दूसरे toy से belong करता है।
Syntax
markup
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.

markup
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.

markup
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.

markup
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. #}
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. यह भूल जाना कि एक ForeignKey के related object को access करना (जैसे tutorial.chapter) हर बार एक नई database query trigger करता है जब तक select_related() उपयोग न हो।
  2. ForeignKey attribute को एक model instance या _id suffix उपयोग करने के बजाय एक raw id (एक integer) assign करने की कोशिश करना।
  3. वह 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 बनाता है।

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.