Anyone experience any thing like this before?

16 views
Skip to first unread message

Stanwin Siow

unread,
Mar 3, 2012, 1:05:32 AM3/3/12
to django...@googlegroups.com
Hello,

I was just tidying up on my project when i discovered a small bug.

in my registration form, i have a total of 7 fields.

the last one being a queryset. 

So the forms.py looks like this:

class RegistrationForm(forms.Form):
    username = forms.RegexField(regex=r'^\w+$',
                                max_length=30,
                                widget=forms.TextInput(attrs=attrs_dict),
                                label=_(u'Username'))
    email = forms.EmailField(widget=forms.TextInput(attrs=dict(attrs_dict,
                                                               maxlength=75)),
                             label=_(u'Email address'))
    first_name =forms.CharField(widget=forms.TextInput(attrs=attrs_dict),label=_(u'First Name')) 
    last_name =forms.CharField(widget=forms.TextInput(attrs=attrs_dict),label=_(u'Last Name'))
    password1 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
                                label=_(u'Password'))
    password2 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
                                label=_(u'Password (again)'))
    keywords = forms.ModelMultipleChoiceField(queryset=Keyword.objects.all())
    #keywords = forms.ModelChoiceField(queryset=Keyword.objects.all())
    
    def clean_username(self):
        try:
            user = User.objects.get(username__iexact=self.cleaned_data['username'])
        except User.DoesNotExist:
            return self.cleaned_data['username']
        raise forms.ValidationError(_(u'This username is already taken. Please choose another.'))

    def clean(self):
        if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:
            if self.cleaned_data['password1'] != self.cleaned_data['password2']:
                raise forms.ValidationError(_(u'You must type the same password each time'))
        return self.cleaned_data
    
    def save(self, profile_callback=None):
        new_user = RegistrationProfile.objects.create_inactive_user(username=self.cleaned_data['username'],password=self.cleaned_data['password1'],email=self.cleaned_data['email'],profile_callback=profile_callback)
new_profile = UserProfile(user=new_user,username=self.cleaned_data['username'], keywords_subscribed=self.cleaned_data['keywords'],first_name=self.cleaned_data['first_name'],last_name=self.cleaned_data['last_name'],email=self.cleaned_data['email'])
new_profile.save()       
        return new_user

However, the output becomes like the screenshot below:

The other problem is when i push it to the production server, only four fields come out instead of seven. Any ideas why?





Best Regards,

Stanwin Siow



creecode

unread,
Mar 3, 2012, 10:52:25 AM3/3/12
to django...@googlegroups.com
Hello Stanwin,


On Friday, March 2, 2012 10:05:32 PM UTC-8, St@n wrote:

Hello,

I was just tidying up on my project when i discovered a small bug.

the last one being a queryset. 

So the forms.py looks like this:

    keywords = forms.ModelMultipleChoiceField(queryset=Keyword.objects.all())
 
However, the output becomes like the screenshot below:

The keyword items being displayed as "Keyword object" are probably because you need to define a __unicode__ method for your Keyword model.

Something like this...

    def __unicode__ ( self ):
   
        return '%s' % self.pk

Obviously you would need to tune that method to match your model definition and what you want displayed.

Toodle-looooooooooooo..............
creecode

Stanwin Siow

unread,
Mar 3, 2012, 8:47:15 PM3/3/12
to django...@googlegroups.com
Thanks creecode.

I totally forgot about that! cheers for pointing that out!


Best Regards,

Stanwin Siow


Reply all
Reply to author
Forward
0 new messages