Login

"Approved" field with timestamp

Author:
miracle2k
Posted:
July 17, 2007
Language:
Python
Version:
.96
Tags:
newforms models fields forms save
Score:
0 (after 0 ratings)

I wanted to make the objects of a particular model approvable and store the timestamp of when that happened. In other frameworks/languages, I used to combined those in one "approved_at" field, which would be NULL if an object was currently unapproved.

I tried different approaches to implement this in django, and this is the best I came up with so far. Basically, the code in setattr makes sure that the field, once set, will not be updated again.

Overriding setattr__() could also be a solution to determining if a field value has changed in save(), a question that seems come up from time to time in #django.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class ApprovedStateField(forms.fields.Field):
    widget = forms.CheckboxInput

    def clean(self, value):
        super(ApprovedStateField, self).clean(value)
        if bool(value):
            return datetime.datetime.now()
        else:
            return None  

class Keyword(models.Model):
    name = models.CharField(maxlength=30)    
    approved_at = ApprovedStateField()
    
    def __setattr__(self, name, value):
        # don't change the approved_at attribute if it is already set
        if (name <> "approved_at") or (not hasattr(self, 'approved_at')) or (self.approved_at is None):
                super(Keyword, self).__setattr__(name, value)

More like this

  1. Querying datetime aware objects in your local timezone by jayliew 2 years, 9 months ago
  2. Timestamps in Model by lindsayrgwatt 6 years, 6 months ago
  3. A slightly better YAML serializer by wapcaplet 3 years, 8 months ago
  4. Admin list_display Ajax by whiteinge 7 years, 1 month ago
  5. Fix for the bad behaviour of GenericForeignKey field by pinkeen 4 years, 1 month ago

Comments

pjanderson (on July 17, 2007):

I'm working on a project where all user-contributed content needs to be approved.

What I came to realize is that this is not a trivial problem. If you model is, let's say, a blog Entry, and to keep things simple :), you decide to store Entry options (enable_comments, is_public, is_shared, is_published, etc) in the same model as Entry content, what will happen if the user decides to change some of the options or content after the object has been approved. How will you decide if the object should remain approved, or should be sent back for approval?

I'm not sure that setting a date as the Model field to denote approval state is the best solution. Perhaps there should be a separate table with options and approval state. Someone could even take it further by logging each approval state change.

I guess it depends on the needs of the program and the approval process, but you might soon realize that your approach has limitations, though it's probably sufficient for a quick and simple representation.

I haven't found the best solution yet, but these are issues I'm dealing with.

#

miracle2k (on July 17, 2007):

pjanderson: I know that depending on one's needs, this can be a complex issue.

However, in my case, I usually want to implement an option to withold a piece of content temporarily before it gets finally published (basically a simple draft machanism), and just use that opportunity to store a timestamp as well, only because it can't hurt ;)

There's usually only a limited group of trusted administrative users, so that's good enough for me most of the time.

#

pjanderson (on July 18, 2007):

Yeah, you're right. If that's the case, simple is probably good enough for you.

For me it's a little bit more complicated, as in an international site, I'll have to deal with more complex, region-based approval process.

My models has been evolving into a more complex solution to accommodate the needs of my clients, but I haven't yet found something that I'm truly happy with.

#

Please login first before commenting.