2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
16. Many-to-one relationships that can be null
|
|
|
|
|
|
|
|
To define a many-to-one relationship that can have a null foreign key, use
|
|
|
|
``ForeignKey()`` with ``null=True`` .
|
|
|
|
"""
|
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
class Reporter(models.Model):
|
2007-08-05 13:14:46 +08:00
|
|
|
name = models.CharField(max_length=30)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def __unicode__(self):
|
2006-05-02 09:31:56 +08:00
|
|
|
return self.name
|
|
|
|
|
|
|
|
class Article(models.Model):
|
2007-08-05 13:14:46 +08:00
|
|
|
headline = models.CharField(max_length=100)
|
2006-05-02 09:31:56 +08:00
|
|
|
reporter = models.ForeignKey(Reporter, null=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('headline',)
|
2006-06-04 08:23:51 +08:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def __unicode__(self):
|
2006-06-04 08:23:51 +08:00
|
|
|
return self.headline
|
|
|
|
|
2006-08-27 21:59:47 +08:00
|
|
|
__test__ = {'API_TESTS':"""
|
2006-05-02 09:31:56 +08:00
|
|
|
# Create a Reporter.
|
|
|
|
>>> r = Reporter(name='John Smith')
|
|
|
|
>>> r.save()
|
|
|
|
|
|
|
|
# Create an Article.
|
|
|
|
>>> a = Article(headline="First", reporter=r)
|
|
|
|
>>> a.save()
|
|
|
|
|
|
|
|
>>> a.reporter.id
|
|
|
|
1
|
|
|
|
|
|
|
|
>>> a.reporter
|
2006-06-04 08:23:51 +08:00
|
|
|
<Reporter: John Smith>
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# Article objects have access to their related Reporter objects.
|
|
|
|
>>> r = a.reporter
|
|
|
|
|
|
|
|
# Create an Article via the Reporter object.
|
|
|
|
>>> a2 = r.article_set.create(headline="Second")
|
|
|
|
>>> a2
|
2006-06-04 08:23:51 +08:00
|
|
|
<Article: Second>
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> a2.reporter.id
|
|
|
|
1
|
|
|
|
|
|
|
|
# Reporter objects have access to their related Article objects.
|
|
|
|
>>> r.article_set.all()
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Article: First>, <Article: Second>]
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> r.article_set.filter(headline__startswith='Fir')
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Article: First>]
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> r.article_set.count()
|
|
|
|
2
|
|
|
|
|
|
|
|
# Create an Article with no Reporter by passing "reporter=None".
|
|
|
|
>>> a3 = Article(headline="Third", reporter=None)
|
|
|
|
>>> a3.save()
|
|
|
|
>>> a3.id
|
|
|
|
3
|
|
|
|
>>> print a3.reporter
|
|
|
|
None
|
|
|
|
|
|
|
|
# Need to reget a3 to refresh the cache
|
|
|
|
>>> a3 = Article.objects.get(pk=3)
|
|
|
|
>>> print a3.reporter.id
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
AttributeError: 'NoneType' object has no attribute 'id'
|
|
|
|
|
|
|
|
# Accessing an article's 'reporter' attribute returns None
|
|
|
|
# if the reporter is set to None.
|
|
|
|
>>> print a3.reporter
|
|
|
|
None
|
|
|
|
|
|
|
|
# To retrieve the articles with no reporters set, use "reporter__isnull=True".
|
|
|
|
>>> Article.objects.filter(reporter__isnull=True)
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Article: Third>]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# Set the reporter for the Third article
|
|
|
|
>>> r.article_set.add(a3)
|
|
|
|
>>> r.article_set.all()
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Article: First>, <Article: Second>, <Article: Third>]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# Remove an article from the set, and check that it was removed.
|
|
|
|
>>> r.article_set.remove(a3)
|
|
|
|
>>> r.article_set.all()
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Article: First>, <Article: Second>]
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> Article.objects.filter(reporter__isnull=True)
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Article: Third>]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# Create another article and reporter
|
|
|
|
>>> r2 = Reporter(name='Paul Jones')
|
|
|
|
>>> r2.save()
|
|
|
|
>>> a4 = r2.article_set.create(headline='Fourth')
|
|
|
|
>>> r2.article_set.all()
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Article: Fourth>]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# Try to remove a4 from a set it does not belong to
|
|
|
|
>>> r.article_set.remove(a4)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2006-06-04 08:23:51 +08:00
|
|
|
DoesNotExist: <Article: Fourth> is not related to <Reporter: John Smith>.
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
>>> r2.article_set.all()
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Article: Fourth>]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# Use descriptor assignment to allocate ForeignKey. Null is legal, so
|
|
|
|
# existing members of set that are not in the assignment set are set null
|
|
|
|
>>> r2.article_set = [a2, a3]
|
|
|
|
>>> r2.article_set.all()
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Article: Second>, <Article: Third>]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# Clear the rest of the set
|
|
|
|
>>> r.article_set.clear()
|
|
|
|
>>> r.article_set.all()
|
|
|
|
[]
|
|
|
|
>>> Article.objects.filter(reporter__isnull=True)
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Article: First>, <Article: Fourth>]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2006-08-27 21:59:47 +08:00
|
|
|
"""}
|