Validation error on TimeZoneField form #1

debrice opened this Issue Feb 13, 2010 · 8 comments

6 participants


I added the TimeZoneField to my Profile model so I can provide correct time record to my users. But when I submit the ModelForm (so I use the defaut field to render the time zone), I cannot save it, I obtain those form errors:

Value <DstTzInfo 'America/Jamaica' KMT-1 day, 18:53:00 STD> is not a valid choice.
Value <DstTzInfo 'America/Los_Angeles' PST-1 day, 16:00:00 STD> is not a valid choice.
(looks like somehow it's returning the object and not the object.unicode() value)

... and this happen what ever input I selected.
I'm using django trunk and pytz has just been installed using easy-install (so it's pretty recent).

class Profile(models.Model):
    # User link (one to one relation)
    user = models.ForeignKey(User, related_name='profile', unique=True)
    sex = models.CharField("Gender", max_length=6, choices=SEX_CHOICES, blank=True)
    date_of_birth = models.DateField(blank=True, null=True, help_text=("( format: yyyy-mm-dd )"))
    time_zone = TimeZoneField()

class ProfileForm(forms.ModelForm):
    class Meta:
        model = Profile
        fields = ('date_of_birth',

def profile_view(request):
    Allow users to add personnal data to their profile
    profile = request.user.get_profile()
    context = {'current':'account'}
    if request.method == "POST":
        profile_form = ProfileForm(instance=profile, data=request.POST)
        user_form = UserForm(instance=request.user, data=request.POST)
        if profile_form.is_valid() and user_form.is_valid():
            context['profile_saved'] = True
        user_form = UserForm(instance=request.user)
        profile_form = ProfileForm(instance=profile)
    context.update({'user_form': user_form,
                    'profile_form': profile_form,
    return render_response(request, 'profile/profile.html', context)

I'm seeing the same thing. If I remove the to_python method completely, it saves the string representation to the database. But I'm not far enough along to know if that's all that's needed in the LocalizedDateTimeField.


is there anybody behind this support page ?


I am not sure how this slipped under my radar, but I have been quite busy. Haven't had the opportunity to sit down a hack on this app in a while. The stars have aligned today and I have been hacking around. My response at this time is that I've reproduced this and I have a test case ready to ensure it doesn't break again. Going to explore why this is and understand it a bit better. A fix is forth-coming. Thanks for hanging in there all! :-)


Closed by ba4485e — handle validation better at the model field level and made validation much more consistent across the form field and model field

This was a doozy. TimeZoneField does not actually use TimeZoneField defined
via formfield because it has choices. Django forces TypedChoiceField. This
at least makes them behave mostly consistetly. Needed to handle validation
at the model field level for 1.2 since we want to deal with a pytz timezone
object normally, but when validating we need to work with its string
representation. Tests are now passing. There may be some extra minor bits
tossed in too for good measure.


Great news ! Thank you Brosner, I'll try it this week !


I have tried to download the code using easy_install but I can not get to the 0.2.0 that seems to have these corrections, can only get 0.1.4?


Bump. How about tag/release of 0.2.0?


Bump bump. How about tag/release of 0.2.0?

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment