I have two tables:
class Advertisement(models.Model): created_at = models.DateTimeField(auto_now_add=True) author_email = models.EmailField() class Verification(models.Model): advertisement = models.ForeignKeyField(Advertisement) key = models.CharField(max_length=32)
And I need to automatically populate the Verification table after adding a new ad.
def gen_key(sender, instance, created, **kwargs): if created: from hashlib import md5 vkey = md5("%s%s" % (instance.author_email, instance.created_at)) ver = Verification(advertisement=instance) ver.key = vkey ver.save() post_save.connect(gen_key, sender=Advertisement)
Of course, this will not work. Django 1.2 Q: How do I do this?
Okay, half allowed.
The problem is that post_save () for the parent model does not call child models.
This way you can solve this problem by providing the child class directly.
class Advertisement(models.Model): created_at = models.DateTimeField(auto_now_add=True) author_email = models.EmailField() class Sale(Advertisement): rooms = models.IntegerField(max_length=1) subway = models.ForeignKey(Subway) class Verification(models.Model): advertisement = models.ForeignKeyField(Advertisement) key = models.CharField(max_length=32) def gen_key(sender, instance, created, **kwargs): code goes here post_save.connect(gen_key, sender=Sale, dispatch_uid="my_unique_identifier")
So the next question: "How can I use the parent class for post_save ()?"
source share