chrischambers / django-audited-models

An abstract base class providing automatic creator/editor/datetime_created/datetime_modified fields.
Other
3 stars 0 forks source link

Creator not popuplated when creating a new model instance #1

Open timlinux opened 11 years ago

timlinux commented 11 years ago

Hi

I am just testing out your code. I got everything set up fine, but when I try to create a new model instance it always shows creator as None - is there something special I should be doing to make is populate creator with the authenticated user?

Thanks

Tim

chrischambers commented 11 years ago

Hey Tim,

The first thing to check is that you've setup the application correctly - in particular, ensuring you've added the threadlocals middleware into your MIDDLEWARE_CLASSES list.

Assuming that's fine, the only requirement is that you need to be logged in. If your new model instance is being created in a view, you can simply use the web interface to login - either your own or the one at /admin. If it's being created via a script, you'll need to manually set/clear the user (you can see an example of this in the test app here. In your testcases, you'll need to use the self.client.login method, or manually set/clear the current user stored in threadlocals, as above.

Hope this helps!

Chris.

timlinux commented 11 years ago

Hi @chrischambers

Thanks for taking the time to reply.

Yes I have threadlocals installed:

https://github.com/timlinux/visual_changelog/blob/master/django_project/core/settings/prod.py#L23

And yes my model instances are being created via a view e.g.

https://github.com/timlinux/visual_changelog/blob/master/django_project/changes/views/entry.py#L92

Poking around a bit it seems that it audit fields are populated under admin ui but not using my create and update views. These views require you to be logged in to use them. Note that I am using django 1.5.1 and CBV. Is there anything else I should be doing?

Thanks

Regards

Tim