2013-04-24 79 views
1

我扩展了我的django用户,现在需要创建一个注册表单。在Django中为扩展用户扩展UserCreationForm

我得到了大部分的想法,但我不知道如何排除在注册期间不需要的字段。我知道我看到注册表格中的所有字段。

下面是代码:

models.py

class Artist(Model): 
    user = OneToOneField(User, unique=True) 
    address = CharField(max_length=50) 
    city = CharField(max_length=30) 
    ustid = CharField(max_length=14) 
    date_of_birth = DateField() 
    bio = CharField(max_length=500) 
    def __unicode__(self): 
     return self.user.get_full_name() 

User.profile = property(lambda u: Artist.objects.get_or_create(user=u)[0]) 

forms.py

class RegistrationForm(UserCreationForm): 
    class Meta: 
     model = User 

    def __init__(self, *args, **kwargs): 
     super(RegistrationForm, self).__init__(*args, **kwargs) 
     artist_kwargs = kwargs.copy() 
     if kwargs.has_key('instance'): 
      self.artist = kwargs['instance'].artist 
      artist_kwargs['instance'] = self.artist 
     self.artist_form = ArtistForm(*args, **artist_kwargs) 
     self.fields.update(self.artist_form.fields) 
     self.initial.update(self.artist_form.initial) 

    def clean(self): 
     cleaned_data = super(RegistrationForm, self).clean() 
     self.errors.update(self.artist_form.errors) 
     return cleaned_data 

    def save(self, commit=True): 
     self.artist_form.save(commit) 
     return super(RegistrationForm, self).save(commit) 

如何排除领域?

回答

0

您不能包含或排除不是元模型成员的字段。

你可以做的是在每种形式中做到这一点。在这种情况下,UserCreationFormArtistForm扩展。只要限制属于正确元模型的表单中的字段即可。

0
class Meta: 
    model = User 
    exclude = ('bio',) 
+0

不起作用。我只能选择属于元模型的字段。但是我可以在我的ArtistForm类中选择它们。 – codingjoe 2013-04-24 17:22:18