Saturday, November 26, 2011

Re: post_save problem

Hi,

Let's assume your first model on which you want to have post_save be
User(django.contrib.auth.models.User). Say, you want to alter the data
of some other model whenever a User is saved i.e post_save for User.
Let's say this model as "MyModel". The code which you want to run on
post_save goes in a file, let's say "code_on_post_save.py". Let's name
the function which contains this code as "post_save_function". So, the
content of code_on_post_save.py becomes as follows

def post_save_function(sender, **kwargs):
abc = MyModel.objects.get(somefield='def')
abc.someotherfield = 'pqr'
.....
.....
abc.save()
......

Now you need to "connect" this code with your "User" model so that it
runs on "post_save" for the User. This can be done in any of your
views. Let's assume that as "views.py" of your app X.

So, in views.py you need to add this code. (You can have this code in
addition to whatever you normally have in your views.py)

from code_on_post_save import post_save_function
from django.db.models.signals import post_save
from django.contrib.auth.models import User

post_save.connect(post_save_function, sender=User)

I hope this suffice.

Thanks,
Akshar
So, in any of your other view, you need to
On Nov 26, 1:46 am, Vitor Duarte Venâncio <vdvenan...@gmail.com>
wrote:
> Greetings.
> I'm trying to do a post_save command in a page oriented to one specific
> class, and make this post_save alter some data that's stored in objects of
> another class.
> Anyone has an example or guidance that can help?
> Thank you very much.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment