Thursday, October 6, 2011

Re: Question about Signals : How to get the Primary Key Value ?


On Thursday, 6 October 2011 10:10:09 UTC+1, bino wrote:
Dear All ..

 From the basic tutorial of "Polls",
I add this lines to model.py
----START--------
from django.db.models.signals import post_save

def do_something(sender, **kwargs):
     print "==========START==========="
     print "Called by : " , sender
     print "This is KWARGS :"
     print kwargs
     obj = kwargs['instance']
     print "This is instances :"
     print obj
     print "=========DONE============"

post_save.connect(do_something, sender=Poll)
---Stop--

And from the debug console while I add a new record, here is what I got :

==========START===========
Called by : <class 'poll.models.Poll'>
This is KWARGS :
{'raw': False, 'signal': <django.dispatch.dispatcher.Signal object at
0x8ddab6c>, 'instance': <Poll: WhatsUp ?>, 'created': True}
This is instances :
WhatsUp ?
=========DONE============

My question : How to het the primary key of involved object/instance
(the "id" Column in table) ?
When I try to use --> print Poll.id
It raise traceback that state (more/less) : Model 'Poll' have no object
named 'id'

Kindly please give me your enlightment

Sincerely
-bino-


The instance is in, well, the `instance` argument. You get it from there.
--
DR. 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/vKHDqRZDP_EJ.
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