Я следую примеру из https://docs.djangoproject.com/en/2.1/topics/db/examples/one_to_one/
Может кто-нибудь помочь объяснить, почему в строках ниже не возвращается Restaurant: Ace Hardware the restaurant
?Вместо этого он возвращает Restaurant: Demon Dogs the restaurant
.Спасибо.
>>> r
<Restaurant: Ace Hardware the restaurant>
>>> p1.restaurant
<Restaurant: Ace Hardware the restaurant>
# Set the place back again, using assignment in the reverse direction:
>>> p1.restaurant = r
>>> p1.restaurant
<Restaurant: Demon Dogs the restaurant>
Код и фактический вывод указаны ниже:
from django.db import models
class Place(models.Model):
name = models.CharField(max_length=50)
address = models.CharField(max_length=80)
def __str__(self):
return "%s the place" % self.name
class Restaurant(models.Model):
place = models.OneToOneField(
Place,
on_delete=models.CASCADE,
primary_key=True,
)
serves_hot_dogs = models.BooleanField(default=False)
serves_pizza = models.BooleanField(default=False)
def __str__(self):
return "%s the restaurant" % self.place.name
>>> p1 = Place(name='Demon Dogs', address='944 W. Fullerton')
>>> p1.save()
>>> p2 = Place(name='Ace Hardware', address='1013 N. Ashland')
>>> p2.save()
>>> r = Restaurant(place=p1, serves_hot_dogs=True, serves_pizza=False)
>>> r.save()
>>> r.place
<Place: Demon Dogs the place>
>>> p1.restaurant
<Restaurant: Demon Dogs the restaurant>
>>> r.place = p2
>>> r.save()
>>> r.place
<Place: Ace Hardware the place>
>>> p2.restaurant
<Restaurant: Ace Hardware the restaurant>
>>> r
<Restaurant: Ace Hardware the restaurant>
>>> p1.restaurant
<Restaurant: Ace Hardware the restaurant>
>>> r.place
<Place: Ace Hardware the place>
>>> p1.restaurant = r
>>> p1.restaurant
<Restaurant: Demon Dogs the restaurant>