Can't display DateField on form with auto_now = True

Go To StackoverFlow.com

1

I have a model with auto_now, and auto_now_add set for Update and Create fields:

class HotelProfiles(models.Model):
  fe_result_id = models.AutoField(primary_key=True)
  fe_created_date = models.DateTimeField(verbose_name='Created',
                                         blank=True,
                                         auto_now_add=True)
  fe_updated_date = models.DateTimeField(verbose_name='Updated',
                                         blank=True, 
                                         auto_now=True)

In the Admin it displays both fields but leaves them uneditable. They don't seem to be passed to my form to be rendered. I don't want them to be editable, but I would like to display at the top of my form. How can I do this?

This is in my HotelProfilesAdmin class:

readonly_fields = ('fe_result_id', 'fe_created_date', 'fe_updated_date', 'fe_owner_uid')
#date_hierarchy = 'lto_end_date' 
fieldsets = (
    ("Internal Use Only", {
        'classes': ('collapse',),
        'fields': ('fe_result_id', 'fe_created_date', 'fe_owner_uid', 'fe_updated_date', 'fe_result_status')
    }),
2012-04-05 17:50
by joel goldstick


1

For the benefit of others, I figured out a way to do this. I'm new to Django, so if there is a better way, I'd be interested in hearing it. The view code is below. I wasn't sure if Django was not returning the fields from the query, and I found out that it was. So, something in the renderering of the form that I don't understand removed those fields so they couldn't be rendered. So, I copied them to a dict called read_only before rendering and passed it along.

try:
    hotel_profile = HotelProfiles.objects.get(pk=hotel_id)
    read_only["created_on"] = hotel_profile.fe_created_date
    read_only["updated_on"] = hotel_profile.fe_updated_date
    f = HotelProfileForm(instance=hotel_profile)
    #f.save()
except:
    f = HotelProfileForm()
    print 'rendering blank form'
return render_to_response('hotels/hotelprofile_form.html', {'f' : f, 'read_only': read_only}, context_instance=RequestContext(request))
2012-04-05 20:08
by joel goldstick


2

  • Make the fields you want readonly
  • explicitly override what fields are available in this admin form (readonly fields will be present but readonly)

Example:

from django.contrib import admin

class HotelProfilesAdmin(admin.ModelAdmin) :
    # Keep the fields readonly
    readonly_fields = ['fe_created_date','fe_updated_date']

    # The fields in the order you want them
    fieldsets = (
        (None, {
            'fields': ('fe_created_date', 'fe_updated_date', ...other fields)
        }),
    )

# Add your new adminform to the site
admin.site.register(HotelProfiles, HotelProfilesAdmin)
2012-04-05 18:01
by Francis Yaconiello
I have already done that. In Admin it works great -- the fields display but are not editable. However in my front facing form It seems that the two fields don't get delivered to the template - joel goldstick 2012-04-05 18:08
gotcha, 1 sec while i cook up an example - Francis Yaconiello 2012-04-05 18:10
Ads