Many-to-Many संबंध
In this page:
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.
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.
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.
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. #}
- एक ManyToManyField को दोनों models में जोड़ना बजाय सिर्फ एक में — इसे सिर्फ एक बार declare करना होता है, किसी भी side पर।
- related objects को = के साथ assign करने की कोशिश करना बजाय manager पर .add(), .set(), या .remove() उपयोग करने के।
- यह भूल जाना कि 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 नहीं।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: