Code

Changes between Version 15 and Version 16 of NewformsHOWTO


Ignore:
Timestamp:
02/20/08 01:07:53 (6 years ago)
Author:
Lllama <f.ingram.lists@…>
Comment:

Changed formfield overrides to use super(...). Added question about created and updated fields

Legend:

Unmodified
Added
Removed
Modified
  • NewformsHOWTO

    v15 v16  
    482482    urania_event_id = UraniaEventID(max_length=4) 
    483483}}} 
     484 
     485 
     486== Q: How do I use !updated_at and !created_on type fields but stop them being displayed? 
     487 
     488You'll need to override a few methods: two to hide the fields and two to populate them with the correct values. 
     489 
     490Let's say you have this model. 
     491 
     492{{{ 
     493#!python 
     494class Article(models.Model): 
     495    headline = models.CharField(max_length=500) 
     496    text = models.TextField() 
     497 
     498    created = models.DateField() 
     499    updated = models.DateTimeField() 
     500}}} 
     501 
     502And you want to use !created and !updated in your list view, but not in the change and add views. 
     503 
     504{{{ 
     505#!python 
     506class ArticleOptions(admin.ModelAdmin): 
     507    save_on_top = True 
     508    list_display = ('headline', 'updated', 'created') 
     509 
     510    hidden_fields = ("created", "updated") # Define which fields you'd like to be hidden here. 
     511 
     512    def fieldsets_change(self, request, obj): 
     513        superclass = super(ArticleOptions, self) 
     514        fieldsets = superclass.fieldsets_change(request, obj) 
     515 
     516        # Here we cycle through the fieldsets and remove the created and updated 
     517        # fields by name. Factoring this out left as an exercise for the reader. 
     518        for fs in fieldsets: 
     519            fs[1]['fields'] = [f for f in fs[1]['fields'] 
     520                                 if f not in self.hidden_fields] 
     521        return fieldsets 
     522 
     523    def fieldsets_add(self, request): 
     524        superclass = super(ArticleOptions, self) 
     525        fieldsets = superclass.fieldsets_add(request) 
     526 
     527        # Here we cycle through the fieldsets and remove the created and updated 
     528        # fields by name. Factoring this out left as an exercise for the reader. 
     529        for fs in fieldsets: 
     530            fs[1]['fields'] = [f for f in fs[1]['fields'] 
     531                                 if f not in self.hidden_fields] 
     532        return fieldsets 
     533 
     534    def add_view(self, request): 
     535        if request.method == "POST": 
     536            # Change the created field to be a datetime object. 
     537            request.POST['created'] = datetime.datetime.today() 
     538            # The admin will be expecting date and time as separate fields 
     539            # as the default widget is rendered as two text fields. 
     540            # You can get away with a date object for the date 
     541            request.POST['updated_0'] = datetime.date.today() 
     542            # But it's easier to poke in a text string for the time (less typing). 
     543            request.POST['updated_1'] = datetime.datetime.now().strftime("%H:%M:%S") 
     544        return super(ArticleOptions, self).add_view(request) 
     545 
     546    def change_view(self, request, obj_id): 
     547        if request.method == "POST": 
     548            old_issue = Article.objects.get(id=obj_id) 
     549            # Here we use the old created date as we're changing, not adding. 
     550            request.POST['created'] = old_issue.created 
     551            # Same as above for the updated time 
     552            request.POST['updated_0'] = datetime.date.today() 
     553            request.POST['updated_1'] = datetime.datetime.now().strftime("%H:%M:%S") 
     554        return super(ArticleOptions, self).change_view(request, obj_id) 
     555}}}