Paul Smith <paul.smith <at> catugmt.com> writes:

> 
> I’ve been trying to a modify an admin form so I have a slightly different
> user interface compared to the database model.
> 
> The main problem is this; in the database model I store a start and end
> time as timestamps (a float holding seconds since 1/1/1970). But when the user
> wants to edit the times I don’t want him to have to deal with big float values
> so I’ve created a new form with DateTimeFields thus;
> 
>   class MyNewForm(forms.ModelForm):
>       start_time = forms.DateTimeField()
>       end_time = forms.DateTimeField()
> 
>       def clean(self):
>           cleaned_data = self.cleaned_data
>           start = cleaned_data.get('start_time')
>           end = cleaned_data.get('end_time')
>           cleaned_data['start_time']=time.mktime(start.timetuple())
> 
>           if end:
>               cleaned_data['end_time']=time.mktime(end.timetuple())
>           return cleaned_data
> 
>       class Meta:
>           model = MyModel
> 
> where MyModel is just;
> 
>   class MyModel(models.Model):
>       start_time = models.FloatField()
>       end_time = models.FloatField(null=True)
> 
> and the admin declaration is;
> 
>   class MyModelAdmin(admin.ModelAdmin):
>       form = MyNewForm
> 
> Now this cleans any new date/time entries and converts them to floats
> before writing to the db just fine, but if the user is editing an existing
> record I can’t get the existing start and end times to display as, for
> example, ‘2009-10-25 12:00’, they always show up as the float
> values. I’ve tried to change this by adding the following __init__ method
> to MyNewForm;
> 
>     def __init__(self, *args, **kwargs):
>         super(MyNewForm, self).__init__(*args, **kwargs)
>         instance=kwargs.get('instance','')
>         if instance:
             self.fields['start_time'] = 
forms.DateTimeField(initial=datetime.fromtimestamp(instance.start_time))        
 
#
> tried this to set the initial value
>             self.fields['start_time'].initial = 
datetime.fromtimestamp(instance.start_time) #
then tried this
> 
> But neither of these seem to override the admin’s rendering of
> the float times and I’m not sure where that gets decided. I haven’t
> seen any examples similar to this but would have thought it was fairly common
> to change the way data is displayed to the user for editing and then convert 
it
> back to another form (via clean) when a change is submitted.
> 
>  
> 
> Paul

Ok, I realised I'd come across this problem elsewhere in my code and forgot
what I did. The __init__ method should look like;

    def __init__(self, *args, **kwargs):
        super(MyNewForm, self).__init__(*args, **kwargs)
        instance=kwargs.get('instance','')
        if instance:
            self.initial['start_time'] = 
               datetime.fromtimestamp(delay_instance.start_time)
            self.initial['end_time'] = 
               datetime.fromtimestamp(delay_instance.end_time)

Then the floats get changed to datetime objects and render properly.



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to