如何在Django一对一关系中显示和更新模型数据?

时间:2018-10-18 22:33:52

标签: python django python-2.7 django-models django-forms

我有两个模型Place和Restaurant具有一对一的关系。我想使用一种模型表单Restaurantform,并在表单编辑模式下显示地名。提交表单后,需要更新两个模型字段中的数据。

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,
   )
   name = models.CharField(max_length=50)
   serves_pizza = models.BooleanField(default=False)

   def __str__(self):
      return "%s the restaurant" % self.place.name

class Restaurantform(forms.ModelForm):
   class Meta:
      model = Restaurant
      fields = ['name', 'serves_pizza', 'Name field from Place Model?']

我不确定如何显示地名和处理表单提交!

谢谢!

0 个答案:

没有答案