Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
D
django
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
django
Commits
43efefae
Kaydet (Commit)
43efefae
authored
Şub 07, 2013
tarafından
Tim Graham
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Fixed #19756 - Corrected a ManyToMany example and added some links and markup.
üst
720888a1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
27 additions
and
20 deletions
+27
-20
many_to_many.txt
docs/topics/db/examples/many_to_many.txt
+27
-20
No files found.
docs/topics/db/examples/many_to_many.txt
Dosyayı görüntüle @
43efefae
...
@@ -35,7 +35,7 @@ objects, and a ``Publication`` has multiple ``Article`` objects:
...
@@ -35,7 +35,7 @@ objects, and a ``Publication`` has multiple ``Article`` objects:
What follows are examples of operations that can be performed using the Python
What follows are examples of operations that can be performed using the Python
API facilities.
API facilities.
Create a couple of
Publications
::
Create a couple of
``Publications``
::
>>> p1 = Publication(title='The Python Journal')
>>> p1 = Publication(title='The Python Journal')
>>> p1.save()
>>> p1.save()
...
@@ -44,11 +44,11 @@ Create a couple of Publications::
...
@@ -44,11 +44,11 @@ Create a couple of Publications::
>>> p3 = Publication(title='Science Weekly')
>>> p3 = Publication(title='Science Weekly')
>>> p3.save()
>>> p3.save()
Create an
Article
::
Create an
``Article``
::
>>> a1 = Article(headline='Django lets you build Web apps easily')
>>> a1 = Article(headline='Django lets you build Web apps easily')
You can't associate it with a
Publication
until it's been saved::
You can't associate it with a
``Publication``
until it's been saved::
>>> a1.publications.add(p1)
>>> a1.publications.add(p1)
Traceback (most recent call last):
Traceback (most recent call last):
...
@@ -60,11 +60,11 @@ Save it!
...
@@ -60,11 +60,11 @@ Save it!
>>> a1.save()
>>> a1.save()
Associate the
Article with a Publication
::
Associate the
``Article`` with a ``Publication``
::
>>> a1.publications.add(p1)
>>> a1.publications.add(p1)
Create another
Article, and set it to appear in both Publications
::
Create another
``Article``, and set it to appear in both ``Publications``
::
>>> a2 = Article(headline='NASA uses Python')
>>> a2 = Article(headline='NASA uses Python')
>>> a2.save()
>>> a2.save()
...
@@ -75,25 +75,26 @@ Adding a second time is OK::
...
@@ -75,25 +75,26 @@ Adding a second time is OK::
>>> a2.publications.add(p3)
>>> a2.publications.add(p3)
Adding an object of the wrong type raises
TypeError
::
Adding an object of the wrong type raises
:exc:`~exceptions.TypeError`
::
>>> a2.publications.add(a1)
>>> a2.publications.add(a1)
Traceback (most recent call last):
Traceback (most recent call last):
...
...
TypeError: 'Publication' instance expected
TypeError: 'Publication' instance expected
Add a Publication directly via publications.add by using keyword arguments::
Create and add a ``Publication`` to an ``Article`` in one step using
:meth:`~django.db.models.fields.related.RelatedManager.create`::
>>> new_publication = a2.publications.create(title='Highlights for Children')
>>> new_publication = a2.publications.create(title='Highlights for Children')
Article objects have access to their related Publication
objects::
``Article`` objects have access to their related ``Publication``
objects::
>>> a1.publications.all()
>>> a1.publications.all()
[<Publication: The Python Journal>]
[<Publication: The Python Journal>]
>>> a2.publications.all()
>>> a2.publications.all()
[<Publication: Highlights for Children>, <Publication: Science News>, <Publication: Science Weekly>, <Publication: The Python Journal>]
[<Publication: Highlights for Children>, <Publication: Science News>, <Publication: Science Weekly>, <Publication: The Python Journal>]
Publication objects have access to their related Article
objects::
``Publication`` objects have access to their related ``Article``
objects::
>>> p2.article_set.all()
>>> p2.article_set.all()
[<Article: NASA uses Python>]
[<Article: NASA uses Python>]
...
@@ -102,7 +103,8 @@ Publication objects have access to their related Article objects::
...
@@ -102,7 +103,8 @@ Publication objects have access to their related Article objects::
>>> Publication.objects.get(id=4).article_set.all()
>>> Publication.objects.get(id=4).article_set.all()
[<Article: NASA uses Python>]
[<Article: NASA uses Python>]
Many-to-many relationships can be queried using :ref:`lookups across relationships <lookups-that-span-relationships>`::
Many-to-many relationships can be queried using :ref:`lookups across
relationships <lookups-that-span-relationships>`::
>>> Article.objects.filter(publications__id__exact=1)
>>> Article.objects.filter(publications__id__exact=1)
[<Article: Django lets you build Web apps easily>, <Article: NASA uses Python>]
[<Article: Django lets you build Web apps easily>, <Article: NASA uses Python>]
...
@@ -119,7 +121,8 @@ Many-to-many relationships can be queried using :ref:`lookups across relationshi
...
@@ -119,7 +121,8 @@ Many-to-many relationships can be queried using :ref:`lookups across relationshi
>>> Article.objects.filter(publications__title__startswith="Science").distinct()
>>> Article.objects.filter(publications__title__startswith="Science").distinct()
[<Article: NASA uses Python>]
[<Article: NASA uses Python>]
The count() function respects distinct() as well::
The :meth:`~django.db.models.query.QuerySet.count` function respects
:meth:`~django.db.models.query.QuerySet.distinct` as well::
>>> Article.objects.filter(publications__title__startswith="Science").count()
>>> Article.objects.filter(publications__title__startswith="Science").count()
2
2
...
@@ -133,7 +136,7 @@ The count() function respects distinct() as well::
...
@@ -133,7 +136,7 @@ The count() function respects distinct() as well::
[<Article: Django lets you build Web apps easily>, <Article: NASA uses Python>]
[<Article: Django lets you build Web apps easily>, <Article: NASA uses Python>]
Reverse m2m queries are supported (i.e., starting at the table that doesn't have
Reverse m2m queries are supported (i.e., starting at the table that doesn't have
a
ManyToManyField
)::
a
:class:`~django.db.models.ManyToManyField`
)::
>>> Publication.objects.filter(id__exact=1)
>>> Publication.objects.filter(id__exact=1)
[<Publication: The Python Journal>]
[<Publication: The Python Journal>]
...
@@ -163,7 +166,7 @@ involved is a little complex)::
...
@@ -163,7 +166,7 @@ involved is a little complex)::
>>> Article.objects.exclude(publications=p2)
>>> Article.objects.exclude(publications=p2)
[<Article: Django lets you build Web apps easily>]
[<Article: Django lets you build Web apps easily>]
If we delete a
Publication, its Articles
won't be able to access it::
If we delete a
``Publication``, its ``Articles``
won't be able to access it::
>>> p1.delete()
>>> p1.delete()
>>> Publication.objects.all()
>>> Publication.objects.all()
...
@@ -172,7 +175,7 @@ If we delete a Publication, its Articles won't be able to access it::
...
@@ -172,7 +175,7 @@ If we delete a Publication, its Articles won't be able to access it::
>>> a1.publications.all()
>>> a1.publications.all()
[]
[]
If we delete an
Article, its Publications
won't be able to access it::
If we delete an
``Article``, its ``Publications``
won't be able to access it::
>>> a2.delete()
>>> a2.delete()
>>> Article.objects.all()
>>> Article.objects.all()
...
@@ -199,7 +202,7 @@ Adding via the other end using keywords::
...
@@ -199,7 +202,7 @@ Adding via the other end using keywords::
>>> a5.publications.all()
>>> a5.publications.all()
[<Publication: Science News>]
[<Publication: Science News>]
Removing
publication from an article
::
Removing
``Publication`` from an ``Article``
::
>>> a4.publications.remove(p2)
>>> a4.publications.remove(p2)
>>> p2.article_set.all()
>>> p2.article_set.all()
...
@@ -242,7 +245,7 @@ And you can clear from the other end::
...
@@ -242,7 +245,7 @@ And you can clear from the other end::
>>> p2.article_set.all()
>>> p2.article_set.all()
[<Article: Oxygen-free diet works wonders>]
[<Article: Oxygen-free diet works wonders>]
Recreate the
article and Publication
we have deleted::
Recreate the
``Article`` and ``Publication``
we have deleted::
>>> p1 = Publication(title='The Python Journal')
>>> p1 = Publication(title='The Python Journal')
>>> p1.save()
>>> p1.save()
...
@@ -250,7 +253,8 @@ Recreate the article and Publication we have deleted::
...
@@ -250,7 +253,8 @@ Recreate the article and Publication we have deleted::
>>> a2.save()
>>> a2.save()
>>> a2.publications.add(p1, p2, p3)
>>> a2.publications.add(p1, p2, p3)
Bulk delete some Publications - references to deleted publications should go::
Bulk delete some ``Publications`` - references to deleted publications should
go::
>>> Publication.objects.filter(title__startswith='Science').delete()
>>> Publication.objects.filter(title__startswith='Science').delete()
>>> Publication.objects.all()
>>> Publication.objects.all()
...
@@ -267,15 +271,18 @@ Bulk delete some articles - references to deleted objects should go::
...
@@ -267,15 +271,18 @@ Bulk delete some articles - references to deleted objects should go::
[<Article: Django lets you build Web apps easily>]
[<Article: Django lets you build Web apps easily>]
>>> q.delete()
>>> q.delete()
After the delete, the QuerySet cache needs to be cleared, and the referenced
After the :meth:`~django.db.models.query.QuerySet.delete`, the
objects should be gone::
:class:`~django.db.models.query.QuerySet` cache needs to be cleared, and the
referenced objects should be gone::
>>> print(q)
>>> print(q)
[]
[]
>>> p1.article_set.all()
>>> p1.article_set.all()
[<Article: NASA uses Python>]
[<Article: NASA uses Python>]
An alternate to calling clear() is to assign the empty set::
An alternate to calling
:meth:`~django.db.models.fields.related.RelatedManager.clear` is to assign the
empty set::
>>> p1.article_set = []
>>> p1.article_set = []
>>> p1.article_set.all()
>>> p1.article_set.all()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment