Django用onetoone归档保存在两个模型中

时间:2019-04-22 12:05:04

标签: django django-views

我是Django的初学者,遇到问题,无法解决

我想将地址保存到其他模型中的每个人

我以这种方式尝试过,但是没有用

对不起,我的英语水平

models.py

class Person(models.Model):
user - models.OneToOneField(User, on_delete=models.CASCADE)
name =  models.CharField(max_length=50)
email = models.EmailField("E-mail", max_length=50, blank=True)
address = models.OneToOneField(AddressPerson, on_delete=models.CASCADE)

class AddressPerson(models.Model)
domicile = models.CharField(max_length=100)
county = models.ForeignKey(County, on_delete=models.PROTECT)
locality = models.ForeignKey(Locality, on_delete=models.PROTECT)

views.py

def add_person(request):
address = AddressForm(request.POST or None)
person = PersonForm(request.POST or None)
if address.is_valid() and person.is_valid():
address.save()
person.save()
return redirect('myapp:add_person'
else:
address = AddressForm()
person = PersonForm()
context = {
'form_p': pacient,
'form_a': adresa
}
return render(request, 'person/add.html',context)

0 个答案:

没有答案