2016-01-03 10:56:22 +00:00
|
|
|
=========================
|
2012-03-14 22:16:46 +00:00
|
|
|
Many-to-one relationships
|
2016-01-03 10:56:22 +00:00
|
|
|
=========================
|
2012-03-14 22:16:46 +00:00
|
|
|
|
2014-08-18 14:30:44 +00:00
|
|
|
To define a many-to-one relationship, use :class:`~django.db.models.ForeignKey`::
|
2012-03-14 22:16:46 +00:00
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
class Reporter(models.Model):
|
|
|
|
first_name = models.CharField(max_length=30)
|
|
|
|
last_name = models.CharField(max_length=30)
|
|
|
|
email = models.EmailField()
|
|
|
|
|
2017-01-18 16:51:29 +00:00
|
|
|
def __str__(self):
|
2014-02-09 11:38:13 +00:00
|
|
|
return "%s %s" % (self.first_name, self.last_name)
|
2012-03-14 22:16:46 +00:00
|
|
|
|
|
|
|
class Article(models.Model):
|
|
|
|
headline = models.CharField(max_length=100)
|
|
|
|
pub_date = models.DateField()
|
2015-07-22 14:43:21 +00:00
|
|
|
reporter = models.ForeignKey(Reporter, on_delete=models.CASCADE)
|
2012-03-14 22:16:46 +00:00
|
|
|
|
2017-01-18 16:51:29 +00:00
|
|
|
def __str__(self):
|
2012-03-14 22:16:46 +00:00
|
|
|
return self.headline
|
|
|
|
|
|
|
|
class Meta:
|
2019-11-04 10:57:53 +00:00
|
|
|
ordering = ['headline']
|
2012-03-14 22:16:46 +00:00
|
|
|
|
|
|
|
What follows are examples of operations that can be performed using the Python
|
|
|
|
API facilities.
|
|
|
|
|
2014-08-18 14:30:44 +00:00
|
|
|
.. highlight:: pycon
|
|
|
|
|
2012-03-14 22:16:46 +00:00
|
|
|
Create a few Reporters::
|
|
|
|
|
|
|
|
>>> r = Reporter(first_name='John', last_name='Smith', email='john@example.com')
|
|
|
|
>>> r.save()
|
|
|
|
|
|
|
|
>>> r2 = Reporter(first_name='Paul', last_name='Jones', email='paul@example.com')
|
|
|
|
>>> r2.save()
|
|
|
|
|
|
|
|
Create an Article::
|
|
|
|
|
2012-09-08 15:00:04 +00:00
|
|
|
>>> from datetime import date
|
|
|
|
>>> a = Article(id=None, headline="This is a test", pub_date=date(2005, 7, 27), reporter=r)
|
2012-03-14 22:16:46 +00:00
|
|
|
>>> a.save()
|
|
|
|
|
|
|
|
>>> a.reporter.id
|
|
|
|
1
|
|
|
|
|
|
|
|
>>> a.reporter
|
|
|
|
<Reporter: John Smith>
|
|
|
|
|
2014-05-19 08:45:55 +00:00
|
|
|
Note that you must save an object before it can be assigned to a foreign key
|
|
|
|
relationship. For example, creating an ``Article`` with unsaved ``Reporter``
|
|
|
|
raises ``ValueError``::
|
|
|
|
|
|
|
|
>>> r3 = Reporter(first_name='John', last_name='Smith', email='john@example.com')
|
2015-07-24 11:51:40 +00:00
|
|
|
>>> Article.objects.create(headline="This is a test", pub_date=date(2005, 7, 27), reporter=r3)
|
2014-05-19 08:45:55 +00:00
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2015-07-24 11:51:40 +00:00
|
|
|
ValueError: save() prohibited to prevent data loss due to unsaved related object 'reporter'.
|
2014-05-19 08:45:55 +00:00
|
|
|
|
2012-03-14 22:16:46 +00:00
|
|
|
Article objects have access to their related Reporter objects::
|
|
|
|
|
|
|
|
>>> r = a.reporter
|
|
|
|
|
|
|
|
Create an Article via the Reporter object::
|
|
|
|
|
2012-09-08 15:00:04 +00:00
|
|
|
>>> new_article = r.article_set.create(headline="John's second story", pub_date=date(2005, 7, 29))
|
2012-03-14 22:16:46 +00:00
|
|
|
>>> new_article
|
|
|
|
<Article: John's second story>
|
|
|
|
>>> new_article.reporter
|
|
|
|
<Reporter: John Smith>
|
|
|
|
>>> new_article.reporter.id
|
|
|
|
1
|
|
|
|
|
2018-07-19 19:47:20 +00:00
|
|
|
Create a new article::
|
2012-03-14 22:16:46 +00:00
|
|
|
|
2018-07-19 19:47:20 +00:00
|
|
|
>>> new_article2 = Article.objects.create(headline="Paul's story", pub_date=date(2006, 1, 17), reporter=r)
|
2012-03-14 22:16:46 +00:00
|
|
|
>>> new_article2.reporter
|
|
|
|
<Reporter: John Smith>
|
|
|
|
>>> new_article2.reporter.id
|
|
|
|
1
|
|
|
|
>>> r.article_set.all()
|
2015-10-05 23:07:34 +00:00
|
|
|
<QuerySet [<Article: John's second story>, <Article: Paul's story>, <Article: This is a test>]>
|
2012-03-14 22:16:46 +00:00
|
|
|
|
|
|
|
Add the same article to a different article set - check that it moves::
|
|
|
|
|
|
|
|
>>> r2.article_set.add(new_article2)
|
|
|
|
>>> new_article2.reporter.id
|
|
|
|
2
|
|
|
|
>>> new_article2.reporter
|
|
|
|
<Reporter: Paul Jones>
|
|
|
|
|
|
|
|
Adding an object of the wrong type raises TypeError::
|
|
|
|
|
|
|
|
>>> r.article_set.add(r2)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2018-04-09 13:12:47 +00:00
|
|
|
TypeError: 'Article' instance expected, got <Reporter: Paul Jones>
|
2012-03-14 22:16:46 +00:00
|
|
|
|
|
|
|
>>> r.article_set.all()
|
2015-10-05 23:07:34 +00:00
|
|
|
<QuerySet [<Article: John's second story>, <Article: This is a test>]>
|
2012-03-14 22:16:46 +00:00
|
|
|
>>> r2.article_set.all()
|
2015-10-05 23:07:34 +00:00
|
|
|
<QuerySet [<Article: Paul's story>]>
|
2012-03-14 22:16:46 +00:00
|
|
|
|
|
|
|
>>> r.article_set.count()
|
|
|
|
2
|
|
|
|
|
|
|
|
>>> r2.article_set.count()
|
|
|
|
1
|
|
|
|
|
|
|
|
Note that in the last example the article has moved from John to Paul.
|
|
|
|
|
|
|
|
Related managers support field lookups as well.
|
|
|
|
The API automatically follows relationships as far as you need.
|
|
|
|
Use double underscores to separate relationships.
|
|
|
|
This works as many levels deep as you want. There's no limit. For example::
|
|
|
|
|
|
|
|
>>> r.article_set.filter(headline__startswith='This')
|
2015-10-05 23:07:34 +00:00
|
|
|
<QuerySet [<Article: This is a test>]>
|
2012-03-14 22:16:46 +00:00
|
|
|
|
|
|
|
# Find all Articles for any Reporter whose first name is "John".
|
2014-01-17 22:27:04 +00:00
|
|
|
>>> Article.objects.filter(reporter__first_name='John')
|
2015-10-05 23:07:34 +00:00
|
|
|
<QuerySet [<Article: John's second story>, <Article: This is a test>]>
|
2012-03-14 22:16:46 +00:00
|
|
|
|
|
|
|
Exact match is implied here::
|
|
|
|
|
|
|
|
>>> Article.objects.filter(reporter__first_name='John')
|
2015-10-05 23:07:34 +00:00
|
|
|
<QuerySet [<Article: John's second story>, <Article: This is a test>]>
|
2012-03-14 22:16:46 +00:00
|
|
|
|
|
|
|
Query twice over the related field. This translates to an AND condition in the
|
|
|
|
WHERE clause::
|
|
|
|
|
2014-01-17 22:27:04 +00:00
|
|
|
>>> Article.objects.filter(reporter__first_name='John', reporter__last_name='Smith')
|
2015-10-05 23:07:34 +00:00
|
|
|
<QuerySet [<Article: John's second story>, <Article: This is a test>]>
|
2012-03-14 22:16:46 +00:00
|
|
|
|
|
|
|
For the related lookup you can supply a primary key value or pass the related
|
|
|
|
object explicitly::
|
|
|
|
|
|
|
|
>>> Article.objects.filter(reporter__pk=1)
|
2015-10-05 23:07:34 +00:00
|
|
|
<QuerySet [<Article: John's second story>, <Article: This is a test>]>
|
2012-03-14 22:16:46 +00:00
|
|
|
>>> Article.objects.filter(reporter=1)
|
2015-10-05 23:07:34 +00:00
|
|
|
<QuerySet [<Article: John's second story>, <Article: This is a test>]>
|
2012-03-14 22:16:46 +00:00
|
|
|
>>> Article.objects.filter(reporter=r)
|
2015-10-05 23:07:34 +00:00
|
|
|
<QuerySet [<Article: John's second story>, <Article: This is a test>]>
|
2012-03-14 22:16:46 +00:00
|
|
|
|
|
|
|
>>> Article.objects.filter(reporter__in=[1,2]).distinct()
|
2015-10-05 23:07:34 +00:00
|
|
|
<QuerySet [<Article: John's second story>, <Article: Paul's story>, <Article: This is a test>]>
|
2012-03-14 22:16:46 +00:00
|
|
|
>>> Article.objects.filter(reporter__in=[r,r2]).distinct()
|
2015-10-05 23:07:34 +00:00
|
|
|
<QuerySet [<Article: John's second story>, <Article: Paul's story>, <Article: This is a test>]>
|
2012-03-14 22:16:46 +00:00
|
|
|
|
|
|
|
You can also use a queryset instead of a literal list of instances::
|
|
|
|
|
|
|
|
>>> Article.objects.filter(reporter__in=Reporter.objects.filter(first_name='John')).distinct()
|
2015-10-05 23:07:34 +00:00
|
|
|
<QuerySet [<Article: John's second story>, <Article: This is a test>]>
|
2012-03-14 22:16:46 +00:00
|
|
|
|
|
|
|
Querying in the opposite direction::
|
|
|
|
|
|
|
|
>>> Reporter.objects.filter(article__pk=1)
|
2015-10-05 23:07:34 +00:00
|
|
|
<QuerySet [<Reporter: John Smith>]>
|
2012-03-14 22:16:46 +00:00
|
|
|
>>> Reporter.objects.filter(article=1)
|
2015-10-05 23:07:34 +00:00
|
|
|
<QuerySet [<Reporter: John Smith>]>
|
2012-03-14 22:16:46 +00:00
|
|
|
>>> Reporter.objects.filter(article=a)
|
2015-10-05 23:07:34 +00:00
|
|
|
<QuerySet [<Reporter: John Smith>]>
|
2012-03-14 22:16:46 +00:00
|
|
|
|
|
|
|
>>> Reporter.objects.filter(article__headline__startswith='This')
|
2015-10-05 23:07:34 +00:00
|
|
|
<QuerySet [<Reporter: John Smith>, <Reporter: John Smith>, <Reporter: John Smith>]>
|
2012-03-14 22:16:46 +00:00
|
|
|
>>> Reporter.objects.filter(article__headline__startswith='This').distinct()
|
2015-10-05 23:07:34 +00:00
|
|
|
<QuerySet [<Reporter: John Smith>]>
|
2012-03-14 22:16:46 +00:00
|
|
|
|
|
|
|
Counting in the opposite direction works in conjunction with distinct()::
|
|
|
|
|
|
|
|
>>> Reporter.objects.filter(article__headline__startswith='This').count()
|
|
|
|
3
|
|
|
|
>>> Reporter.objects.filter(article__headline__startswith='This').distinct().count()
|
|
|
|
1
|
|
|
|
|
|
|
|
Queries can go round in circles::
|
|
|
|
|
|
|
|
>>> Reporter.objects.filter(article__reporter__first_name__startswith='John')
|
2015-10-05 23:07:34 +00:00
|
|
|
<QuerySet [<Reporter: John Smith>, <Reporter: John Smith>, <Reporter: John Smith>, <Reporter: John Smith>]>
|
2012-03-14 22:16:46 +00:00
|
|
|
>>> Reporter.objects.filter(article__reporter__first_name__startswith='John').distinct()
|
2015-10-05 23:07:34 +00:00
|
|
|
<QuerySet [<Reporter: John Smith>]>
|
2014-01-17 22:27:04 +00:00
|
|
|
>>> Reporter.objects.filter(article__reporter=r).distinct()
|
2015-10-05 23:07:34 +00:00
|
|
|
<QuerySet [<Reporter: John Smith>]>
|
2012-03-14 22:16:46 +00:00
|
|
|
|
|
|
|
If you delete a reporter, his articles will be deleted (assuming that the
|
|
|
|
ForeignKey was defined with :attr:`django.db.models.ForeignKey.on_delete` set to
|
|
|
|
``CASCADE``, which is the default)::
|
|
|
|
|
|
|
|
>>> Article.objects.all()
|
2015-10-05 23:07:34 +00:00
|
|
|
<QuerySet [<Article: John's second story>, <Article: Paul's story>, <Article: This is a test>]>
|
2012-03-14 22:16:46 +00:00
|
|
|
>>> Reporter.objects.order_by('first_name')
|
2015-10-05 23:07:34 +00:00
|
|
|
<QuerySet [<Reporter: John Smith>, <Reporter: Paul Jones>]>
|
2012-03-14 22:16:46 +00:00
|
|
|
>>> r2.delete()
|
|
|
|
>>> Article.objects.all()
|
2015-10-05 23:07:34 +00:00
|
|
|
<QuerySet [<Article: John's second story>, <Article: This is a test>]>
|
2012-03-14 22:16:46 +00:00
|
|
|
>>> Reporter.objects.order_by('first_name')
|
2015-10-05 23:07:34 +00:00
|
|
|
<QuerySet [<Reporter: John Smith>]>
|
2012-03-14 22:16:46 +00:00
|
|
|
|
|
|
|
You can delete using a JOIN in the query::
|
|
|
|
|
|
|
|
>>> Reporter.objects.filter(article__headline__startswith='This').delete()
|
|
|
|
>>> Reporter.objects.all()
|
2015-10-05 23:07:34 +00:00
|
|
|
<QuerySet []>
|
2012-03-14 22:16:46 +00:00
|
|
|
>>> Article.objects.all()
|
2015-10-05 23:07:34 +00:00
|
|
|
<QuerySet []>
|