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

Many-to-Many संबंध

एक many-to-many relationship students और clubs जैसा है — एक student कई clubs join कर सकता है, और एक club में कई students हो सकते हैं।
Syntax
markup
class ModelA(models.Model):
    others = models.ManyToManyField(ModelB, related_name='as')

a.others.add(b)
a.others.remove(b)
a.others.all()

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

एक model पर declared एक ManyToManyField दोनों models को automatically दूसरे की कई rows से relate होने का एक तरीका देता है।

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

Each Tutorial can have many Tag objects, and each Tag can be linked to many Tutorial objects.

markup
from django.db import models

class Tag(models.Model):
    name = models.CharField(max_length=50)

class Tutorial(models.Model):
    title = models.CharField(max_length=200)
    tags = models.ManyToManyField(Tag, related_name='tutorials')
{# 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. #}

Relations जोड़ना और हटाना

क्योंकि एक ManyToManyField relations का एक set manage करता है, आप plain assignment के बजाय manager methods उपयोग करते हैं।

उदाहरण: Adding and Removing Relations

add() links the tag to the tutorial immediately; remove() unlinks it — both write to the hidden join table right away.

markup
from core.models import Tag

tutorial = Tutorial.objects.first()
tag = Tag.objects.get(name='beginner')

tutorial.tags.add(tag)
tutorial.tags.remove(tag)
{# 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-to-Many Field के Across Query करना

आप एक ManyToManyField के through बिल्कुल एक ForeignKey जैसे filter कर सकते हैं, related model की fields तक पहुंचने के लिए double underscores उपयोग करते हुए।

उदाहरण: Querying Across a Many-to-Many Field

This joins through the hidden many-to-many table to find every Tutorial linked to a Tag named beginner.

markup
from core.models import Tutorial

# Tutorials tagged 'beginner'
Tutorial.objects.filter(tags__name='beginner')
{# 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. एक ManyToManyField को दोनों models में जोड़ना बजाय सिर्फ एक में — इसे सिर्फ एक बार declare करना होता है, किसी भी side पर।
  2. related objects को = के साथ assign करने की कोशिश करना बजाय manager पर .add(), .set(), या .remove() उपयोग करने के।
  3. यह भूल जाना कि ManyToManyField changes .add()/.remove() call करने पर तुरंत save हो जाते हैं, tutorial.save() की ज़रूरत के बिना।
चैप्टर सारांश
  • ManyToManyField दोनों sides की rows को दूसरी side की कई rows से relate होने देता है।
  • Django relationships store करने के लिए पर्दे के पीछे एक hidden join table बनाता है।
  • relationship manage करने के लिए .add(), .remove(), और .set() उपयोग करें, direct assignment नहीं।

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.