我有一個帶有 ManyToMany 字段的模型,每次創(chuàng)建對象時,我都需要使用之前創(chuàng)建的相同電話或電子郵件在數(shù)據(jù)庫中搜索更多對象?,F(xiàn)在,我正在使用 post_save 信號來做到這一點,但我想知道是否有更好的方法來做到這一點,也許在創(chuàng)建序列化程序上。class Leads(models.Model): name = models.CharField(max_lenght=40) phone = models.CharField(max_lenght=14) email = models.EmailField() other_leads = models.ManyToManyField('Leads') created = models.DateTimeField(auto_now_add) @receiver(post_save, sender=Leads) def add_related(sender, instance, created, **kwargs) if created: [instance.other_leads.add(lead) for lead in Leads.objects.filter(email=instance.email, created__lt=instance.created)]
添加回答
舉報
0/150
提交
取消