0

我有一个名为 Genre(三级层次结构)的树结构,它与用户多对多相关。用户选择流派叶节点。以下是我的model.py

class Genre(MPTTModel):
    name = models.CharField(max_length=50, unique=True)
    parent = TreeForeignKey('self', null=True, blank=True, related_name='children')

    def __unicode__(Self):
        return Self.name

    class MPTTMeta:
        order_insertion_by = ['name']


class UserProfile(BaseModel):
    .....
    genre = models.ManyToManyField(Genre)
    zipcode = models.PositiveIntegerField(default=0)
    about = models.CharField(verbose_name=_('Description'), blank=True)

我为每个树叶节点分别创建了表单字段,因为我想自定义表单中的树渲染。它看起来像这样

class SignupFormStep2(forms.Form):  
   class Meta:
        model = User
        fields = ('date_of_birth', 'gender', 'zipcode',)

    first_name = forms.CharField(label=_('First name'), max_length = 25, initial='', widget=forms.TextInput(attrs={'readonly':'readonly'}))
    last_name = forms.CharField(label=_('Last name'), max_length = 25, initial='', widget=forms.TextInput(attrs={'readonly':'readonly'}))
    email = forms.EmailField(label=_('Email'), max_length = 255, initial='', widget=forms.TextInput(attrs={'readonly':'readonly', 'disabled':True}))
    about = forms.CharField(label=_('Description'), initial='')
    cat=range(0,Genre.tree.filter(level=0).count())
    for x in range(0, len(cat)):
        cat[x]=Genre.tree.filter(level=0)[x]

    subcat=[None]*len(cat)
    for x in range(0, len(cat)):
        subcat[x]=cat[x].get_children()

    genre_0_0=forms.ModelMultipleChoiceField(queryset=cat[0].get_children()[0].get_children(),widget=forms.CheckboxSelectMultiple)

    genre_1_0=forms.ModelMultipleChoiceField(queryset=cat[1].get_children()[0].get_children(),widget=forms.CheckboxSelectMultiple)
    genre_1_1=forms.ModelMultipleChoiceField(queryset=cat[1].get_children()[1].get_children(),widget=forms.CheckboxSelectMultiple)
    genre_1_2=forms.ModelMultipleChoiceField(queryset=cat[1].get_children()[2].get_children(),widget=forms.CheckboxSelectMultiple)
    genre_1_3=forms.ModelMultipleChoiceField(queryset=cat[1].get_children()[3].get_children(),widget=forms.CheckboxSelectMultiple)


    genre_2_0=forms.ModelMultipleChoiceField(queryset=cat[2].get_children()[0].get_children(),widget=forms.CheckboxSelectMultiple)
    genre_2_1=forms.ModelMultipleChoiceField(queryset=cat[2].get_children()[1].get_children(),widget=forms.CheckboxSelectMultiple)

我也在使用向导表单进行多注册,但这不是问题。我的观点看起来像这样

class SignupWizard(SessionWizardView):

    def get_form_prefix(self, step=None, form=None):
        return ''


    def get_template_names(self):
        return [SIGNUP_TEMPLATES[self.steps.current]]        

    def save_user(self, form_list, **kwargs):
        print 'save_user'
        form_data = [form.cleaned_data for form in form_list]

        session_key = self.request.session.session_key
        try:
            image = UploadProfileImage.objects.get(key=session_key).image
        except UploadProfileImage.DoesNotExist:
            pass

        user = User()

        password = form_data[0]['password1']
        if password:
            user.set_password(password)
        else:
            user.set_unusable_password()

        user.is_active = True
        user.username = (form_data[0]['email']) #temp maybe add user field??
        user.first_name = form_data[0]['first_name']
        user.last_name = form_data[0]['last_name']
        user.email = form_data[0]['email']
        user.save()

        profile = user.profile        

        profile.date_of_birth = form_data[1].get('date_of_birth')
        profile.zipcode = form_data[1].get('zipcode', 0)    
        #profile.genre = form_data[2].save
        profile.genre = Genre.
        #availability
        return user

我被卡住了,因为我不知道如何将所有表单字段(为单个树叶节点生成)保存到视图中的单个用户模型属性类型中。

知道如何合并许多表单字段并保存到多对多模型属性中。

提前致谢。

4

0 回答 0