I remedied this problem by actually doing all of my work in the
save_model() method. It has all the initial data set on the model
which allowed me to check which values changed (date and/or user) and
set audits as needed.

On Aug 12, 4:06 pm, vjimw <im.a.machobea...@gmail.com> wrote:
> 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.userdata
> 2) On modify, auto populate the last_edited_by field with the
> request.userdata
> 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 editformis 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 currentformsubmit. For this we are
> overriding thesave() method for the model object class
> Offer(models.Model).
>
> To keep track of the original objectdata(versus theformsubmitteddata) 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 theform, this works
> like a charm. We can compare self.end_date with
> self.__original_end_date and see if thedatahas changed. For the
> last_edited_by field, which is not updated in theformbut 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 populateddatahere 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 setdatafrom
> save_model() but the original value for the last_edited_by field.  I
> also noticed that my modelsave() 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 samesave() 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