Note that there are some explanatory texts on larger screens.

plurals
  1. POMany to many field not shown in object while using signals to detect save operation in django
    primarykey
    data
    text
    <p>This is a follow up question to : <a href="https://stackoverflow.com/questions/17257910/cant-get-post-save-to-work-in-django/17259611?noredirect=1#17259611">Cant get post_save to work in Django</a></p> <p><strong>My models are :</strong> </p> <pre><code>class Car(models.Model): name = models.CharField(max_length=50) ... some other attributes of Car ... class Person(models.Model): car = models.ManyToManyField(Car) name = models.CharField(max_lenght=100) ... Some other attributes of Person ... class License(models.Model): person = models.ForeignKey(Person) ... Other attributes of License ... </code></pre> <p><strong>Signal handler:</strong></p> <pre><code>def signal_handler(sender, **kwargs): print 'Person saved!' generate_license() post_save.connect(signal_handler, sender=Person, dispatch_uid="Unique person") </code></pre> <p><strong>The intent:</strong> When an instance of Person is created, I want to generate a License object. So I filter out the last tuple that was added into License and then use its contents to generate a License instance.</p> <pre><code>def generate_license(): import pdb pdb.set_trace() man = Person.objects.filter().order_by('-time_added')[:1][0] # Restricting the filter to one query and then taking the value at the 0th index. order_by '-time_added' gives the latest tuple at the top. license = License.objects.create(...Info about car, person...) </code></pre> <p><strong>The Error:</strong></p> <p>An example: Say <code>Car</code> has 3 instances: </p> <ol> <li>BMW</li> <li>FERRARI</li> <li>LAMBORGHINI</li> </ol> <p>Now when I add an instance of Person from the admin, example:</p> <p><code>per</code> be an instance with <code>car = BMW, FERRARI</code> and <code>name = Bob</code> When I click <code>save</code> in admin, the <code>set_trace()</code> starts. So after the query in <code>generate_license</code> :</p> <p>In <code>pdb</code>, when the query executes, I try printing out <code>per.car.all()</code> but it gives me <code>[]</code> and when I try printing out <code>per.name</code>, it does print out <code>Bob</code>. So I am not really getting as to how <code>per.name</code> is saved and <code>per.car</code> isnt.</p> <p>Also, when the request is completed, that is I have pressed <code>c</code> in <code>pdb</code>, I again click on save for the same instance, this time it reads the <code>per.car.all()</code> perfectly while if before saving, I added <code>LAMBORGHINI</code>, it only shows up <code>BMW</code> and <code>FERRARI</code>. So what I guess is happening is that the <code>many-to-many</code> field is coming a request late. Although I can't point out the reason for this. Need some help. Am I missing something?</p> <p><strong>Question:</strong> Is there a specific way to identify an <code>update signal</code> from a <code>create signal</code>? I mean I do not want to generate a new <code>License</code> every time a data is updated. A new <code>License</code> will only generate when the data has been created. So, how to distinguish between <code>update</code> and <code>save</code> signals?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload