我有一个带有 ManyToMany 字段的模型,每次创建对象时,我都需要使用之前创建的相同电话或电子邮件在数据库中搜索更多对象。
现在,我正在使用 post_save 信号来做到这一点,但我想知道是否有更好的方法来做到这一点,也许在创建序列化程序上。
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)]
相关分类