djangotimefield

Django : duration in time


Currently, I use django 1.4.8 and I have in my model the following two fields:

class Event(models.Model):
...
    start = models.DateTimeField(_(u"départ"))
    end = models.DateTimeField(_('fin'))

I tried to add a field indicating instead the duration in time

     start = models.DateTimeField(_(u"départ"))
     time_delay = models.TimeField(_(u"Time_delay"), auto_now_add=True, blank=True, default=datetime.datetime.utcnow)

but I get the error in the admin

'EventAdmin.fieldsets[0][1]['fields']' refers to field 'time_delay' that is missing from the form

My admin :

class EventAdmin(admin.ModelAdmin):

list_display = ('title', 'start', 'contact_time', 'user', 'fin', 'frequency', 'one_file_',)

fieldsets = (
    (None, {
        'fields': ('title','start', 'contact_time', 'is_cancelled', 'calendar', 'user', 'description', ('frequency', 'fin' ), 'activated', 'one_file',)
    }),
)

....

what is this problem


Solution

  • You set auto_now_add = True for time_delay field so django will not add this field to the form

    class EventAdmin(admin.ModelAdmin):
    
        list_display = ('title', 'start', 'contact_time', 'user', 'fin', 'frequency', 'one_file_',)
    
        fieldsets = (
        (None, {
            'fields': ('title','start', 'contact_time', 'is_cancelled', 'calendar', 'user', 'description', ('frequency', 'fin' ), 'activated', 'one_file',)
        }),
    )
        readonly_fields = ("time_delay",)