offcial documentation:
https://docs.djangoproject.com/en/5.0/topics/db/examples/many_to_many/
from django.db import models class Publication(models.Model): title = models.CharField(max_length=30) class Meta: ordering = ["title"] def __str__(self): return self.title class Article(models.Model): headline = models.CharField(max_length=100) publications = models.ManyToManyField(Publication) class Meta: ordering = ["headline"] def __str__(self): return self.headline
>>> p1 = Publication(title="The Python Journal")
>>> p1.save()
a1 = Article(headline="Django lets you build web apps easily")a1.save()a1.publications.add(p1)//queryArticle.objects.filter(publications=1)-----------------------------------DOT WALKING>>> Publication.objects.filter(article__id=1)
>>> Publication.objects.filter(article__pk=1)Publication
No comments:
Post a Comment