We have a model object (Offer) which has a number of business rules.
1) On create, auto populate the created_by field with the request.user
data
2) On modify, auto populate the last_edited_by field with the
request.user data
3) If a date value change on modify, audit with the object, date and
last_edited_by user

The created_by and last_modified_by values are being populated
automatically in save_model() in the admin for Offer using class
OfferAdmin(admin.ModelAdmin)

def save_model(self, request, obj, form, change):
        # If add, set created_by...
        # For both add and change, update last_edited_by
        obj.last_edited_by_id = request.user
        # A few other code updates
        obj.save()

This works just fine!

The next set of business rules are based around a date changing when
an edit form is submitted.  If the date entered by the user changes,
and matches some rules, we want to audit this and tag it with the
last_edited_by user for that current form submit. For this we are
overriding the save() method for the model object class
Offer(models.Model).

To keep track of the original object data (versus the form submitted
data) I am using the __init__ method on the model like this:

    def __init__(self, *args, **kwargs):
        super(Offer, self).__init__(*args, **kwargs)
        self.__original_end_date = self.end_date
        self.__original_last_edited_by = self.last_edited_by

For the date, which is always manipulated through the form, this works
like a charm. We can compare self.end_date with
self.__original_end_date and see if the data has changed. For the
last_edited_by field, which is not updated in the form but is set in
save_model() we actually get the original value and not the value set
in save_model().

Any idea how I can get my new auto populated data here instead of the
original value?

I looked at the Django admin code and see that save_model() just calls
obj.save() so I am not sure why I don't see my set data from
save_model() but the original value for the last_edited_by field.  I
also noticed that my model save() override method is called twice. The
first time with the original date and the new date, but with
last_edited_by ONLY being the original value and then AGAIN with the
dates both set to the new date but with the original last_edited_by
and new last_edited_by... so I get the change but I just don't get
both changes in the same save() call which means I cannot test for
both conditions.

At the end of the day, the correct user for the last_edited_by field
ends up in the admin tool and database, but the order in which items
are called is throwing me.

Thanks!

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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