mirror of
https://github.com/django/django.git
synced 2025-07-05 18:29:11 +00:00
magic-removal: Fixed assignment notation on descriptors for one-to-one fields.
git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2564 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
9474b37bdf
commit
2e6bd70a17
@ -97,7 +97,10 @@ class SingleRelatedObjectDescriptor(object):
|
|||||||
if instance is None:
|
if instance is None:
|
||||||
raise AttributeError, "%s must be accessed via instance" % self.related.opts.object_name
|
raise AttributeError, "%s must be accessed via instance" % self.related.opts.object_name
|
||||||
# Set the value of the related field
|
# Set the value of the related field
|
||||||
setattr(value, self.related.field.attname, instance)
|
setattr(value, self.related.field.rel.get_related_field().attname, instance)
|
||||||
|
|
||||||
|
# Set the cache on the provided object to point to the new object
|
||||||
|
setattr(value, self.related.field.get_cache_name(), instance)
|
||||||
|
|
||||||
class ReverseSingleRelatedObjectDescriptor(object):
|
class ReverseSingleRelatedObjectDescriptor(object):
|
||||||
# This class provides the functionality that makes the related-object
|
# This class provides the functionality that makes the related-object
|
||||||
|
@ -55,9 +55,33 @@ Traceback (most recent call last):
|
|||||||
...
|
...
|
||||||
DoesNotExist: Restaurant does not exist for {'place__pk': ...}
|
DoesNotExist: Restaurant does not exist for {'place__pk': ...}
|
||||||
|
|
||||||
|
# Set the place using assignment notation. Because place is the primary key on Restaurant,
|
||||||
|
# the save will create a new restaurant
|
||||||
|
>>> r.place = p2
|
||||||
|
>>> r.save()
|
||||||
|
>>> p2.restaurant
|
||||||
|
Ace Hardware the restaurant
|
||||||
|
>>> r.place
|
||||||
|
Ace Hardware the place
|
||||||
|
|
||||||
|
# Set the place back again, using assignment in the reverse direction
|
||||||
|
# Need to reget restaurant object first, because the reverse set
|
||||||
|
# can't update the existing restaurant instance
|
||||||
|
>>> p1.restaurant = r
|
||||||
|
>>> r.save()
|
||||||
|
>>> p1.restaurant
|
||||||
|
Demon Dogs the restaurant
|
||||||
|
|
||||||
|
>>> r = Restaurant.objects.get(pk=1)
|
||||||
|
>>> r.place
|
||||||
|
Demon Dogs the place
|
||||||
|
|
||||||
# Restaurant.objects.all() just returns the Restaurants, not the Places.
|
# Restaurant.objects.all() just returns the Restaurants, not the Places.
|
||||||
|
# Note that there are two restaurants - Ace Hardware the Restaurant was created
|
||||||
|
# in the call to r.place = p2. This means there are multiple restaurants referencing
|
||||||
|
# a single place...
|
||||||
>>> Restaurant.objects.all()
|
>>> Restaurant.objects.all()
|
||||||
[Demon Dogs the restaurant]
|
[Demon Dogs the restaurant, Ace Hardware the restaurant]
|
||||||
|
|
||||||
# Place.objects.all() returns all Places, regardless of whether they have
|
# Place.objects.all() returns all Places, regardless of whether they have
|
||||||
# Restaurants.
|
# Restaurants.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user