从django表格中排除外键字段

时间:2014-12-03 11:00:30

标签: django

假设以下型号。

class Address(models.Model):
    house_number = models.CharField(max_length=50)
    street_1 = models.CharField(max_length=500)
    street_2 = models.CharField(max_length=500)
    town = models.CharField(max_length=500)
    postal_area = models.CharField(max_length=500)
    country = models.CharField(max_length=500)

class RegisteredUser(User):
    address = models.ForeignKey(Address)        
    registered_date = models.DateTimeField('registered_date')

我的问题是如何从Django表单中排除地址字段。目前,它提供了一个选择地址的列表框。

我的表格代码是:

class CreateRegUserForm(forms.ModelForm):
    confirm_email = forms.EmailField(
        label="Confirm email",
        required=True,
    )

    class Meta:
        model = RegisteredUser
        exclude = ('password','last_login','is_superuser','is_staff','is_active','date_joined','registered_date','address_id',)

1 个答案:

答案 0 :(得分:1)

只需在排除列表中添加地址

即可
class CreateRegUserForm(forms.ModelForm):
    confirm_email = forms.EmailField(
        label="Confirm email",
        required=True,
    )

    class Meta:
        model = RegisteredUser
        exclude = ('address','password','last_login','is_superuser','is_staff','is_active','date_joined','registered_date','address_id',)