Django模型与OneToOne关系? [英] Django models with OneToOne relationships?

查看:111
本文介绍了Django模型与OneToOne关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我使用默认的 auth.models.User 加上我的自定义个人资料地址类型如下:

  class Profile(models.Model):
user = models.OneToOneField(User)
primary_phone = models.CharField(max_length = 20)
address = models.ForeignKey(Address)

class Address
country = CountryField(default ='CA')
province = CAProvinceField(default ='BC')
city = models.CharField(max_length = 80)
postal_code = models.CharField(max_length = 6)
street1 = models.CharField(max_length = 80)
street2 = models.CharField(max_length = 80,blank = True,null = True)
street3 = models.CharField(max_length = 80,blank = True,null = True)

现在我想创建一个注册表单。我可以根据 User 创建一个 ModelForm ,但不包括 Profile的字段地址(需要)。那么建立这个表单最好的办法是什么?我是否应该使用 ModelForm



此外,我如何使用相同的表单来编辑复杂的对象?我可以很容易地将Profile的一个实例传回给它,它包含了必要的Address和Profile对象的引用,但是如何让它填充我的字段?

解决方案

如何使用3个独立的 ModelForm 。一个用于地址,一个用于用户,另一个用于个人资料但是与:

  class ProfileForm(ModelForm):
class Meta:
model = Profile
exclude =('user','address',)

然后分别处理这3个表单在你的意见具体来说,对于 ProfileForm 使用保存 commit = False 要更新实例上的用户地址字段:

 #... 
profile_form = ProfileForm(request.POST)
如果profile_form.is_valid():
profile = profile_form.save(commit = False )
#`user`和`address`以前创建过
#通过保存其他表单
profile.user = user
profile.address = address

请务必在此处使用交易,以确保仅当3个表单有效时才会插入行。


Let's say I'm using the default auth.models.User plus my custom Profile and Address models which look like this:

class Profile(models.Model):
    user = models.OneToOneField(User)
    primary_phone = models.CharField(max_length=20)
    address = models.ForeignKey("Address")

class Address(models.Model):
    country = CountryField(default='CA')
    province = CAProvinceField(default='BC')
    city = models.CharField(max_length=80)
    postal_code = models.CharField(max_length=6)
    street1 = models.CharField(max_length=80)
    street2 = models.CharField(max_length=80, blank=True, null=True)
    street3 = models.CharField(max_length=80, blank=True, null=True)

Now I want to create a registration form. I could create a ModelForm based on User but that won't include fields for the Profile and Address (which are required). So what's the best way to go about building this form? Should I even use ModelForm at all?

Furthermore, how would I use the same form for editing the complex object? I could easily pass an instance of Profile back to it, which holds references to the necessary Address and Profile objects, but how do I get it to fill in the fields for me?

解决方案

What about using 3 separate ModelForm. One for Address, one for User, and one for Profile but with :

class ProfileForm(ModelForm):
  class Meta:
    model = Profile
    exclude = ('user', 'address',)

Then, process these 3 forms separately in your views. Specifically, for the ProfileForm use save with commit=False to update user and address field on the instance :

# ...
profile_form = ProfileForm(request.POST)
if profile_form.is_valid():
  profile = profile_form.save(commit=False)
  # `user` and `address` have been created previously
  # by saving the other forms
  profile.user = user
  profile.address = address

Don't hesitate to use transactions here to be sure rows get inserted only when the 3 forms are valid.

这篇关于Django模型与OneToOne关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆