我确实需要在我正在使用bulk_create的单个查询中创建并保存带有餐厅的多个地点,但是它不会保存,请注意该地点,并且仅保存没有F.K的餐厅
我成功地在循环中分别创建了它们,并一一保存,这导致数据库查询过多,而place字段将id作为自动增量
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)
## what I tried
restaurants = []
place1 = place(name = 'name1',address ='address1')
restaurant1 = restaurant(place=place1, serves_hot_dogs=True,serves_pizza=False)
restaurants.append(restaurant1)
place2 = place(name = 'name1',address ='address1')
restaurant2 = restaurant(place=place1, serves_hot_dogs=True,serves_pizza=False)
restaurants.append(restaurant2)
restaurant.bulk_create(restaurants,ignore_conflicts=True)
预期结果::将获得位置为f.k的餐厅对象 实际结果::只有餐厅没有F.K