Saturday, November 26, 2011

Re: post_save problem

Thank you very much for the answer, but I'm still having issues.

I've declared the funcion on the class that will be monitored for changing, let's say, MyModelA. And then, in the view that controls the changes for objects of the model I want to change, in this case MyModelB, I've used the post_save.connect, just as you told me to.

But whe I try to do any sanity checks (i.e print('test')) in the post_save_function, I end up with nothing. I don't get any error messages, but the function is just not being called when I issue a save for MyModelA. 

I've tried relocating the connect code in the views.py archive, but nothing seems to make it work. How can I proceed?

On Sat, Nov 26, 2011 at 11:54 AM, akshar raaj <raaj.akshar@gmail.com> wrote:
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.




--
Vitor Duarte Venâncio
Graduando em Ciência da Computação
Estagiario - FICRM

--
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