0

I have tree structure called Genre(three level hierarchy) which is related many to many with the user. The user selects the genre leaf nodes. The following is my 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)

I created form fields for each of the trees leaf nodes separately as I want to customize the tree rendering in the forms. And it looks like this

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)

I am also using wizard form for multi signup, but thats not the issue. My views looks like this

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

I am stuck as I dont know how to save all the form fields(generated for individual tree leaf nodes) into single user model attribute genre in views.

Any idea how to merge many form fields and save into a many to many model attribute.

Thanks in Advance.

name
  • 571
  • 1
  • 7
  • 11
  • maybe try implementing the clean() function in the form and merge your values into the correct cleaned_data key. – Thomas Jul 08 '13 at 02:06
  • Another alternative may be to use Django's MultiWidget, which can handle the splitting and re-joining of aggregate data into widgets for display. – Matthew Schinckel Jul 08 '13 at 03:40

0 Answers0