说我有
class Address(models.Model):
street = models.CharField()
postal_code = models.CharField()
class Person(models.Model):
name = models.CharField()
address = models.ForeignKey('Address')
如何使用postal_code获取预先存在的Address
,然后将其分配给新的Person
?
我想如果我在插入新的Address
时也明白了如何操作但是 - 如何在保存Address
之前获取Person
? (有postal_code验证和所有)
答案 0 :(得分:0)
我认为你不想在外键的地址周围 这是你的意思吗?
if request.method == 'POST':
form = YourForm(request.POST)
if form.is_valid():
address = Address.objects.get(postal_code=form.cleaned_data["postal_code"])
Person(someStreetVar, address)
Person.save()