Django ManyToMany model validation

前端 未结 3 926
醉梦人生
醉梦人生 2020-12-02 20:30

I have a model with a ManyToManyField similar to this one (the model Word has a language, too):

class Sentence(models.Model):
    words = models.ManyToManyFi         


        
3条回答
  •  囚心锁ツ
    2020-12-02 20:33

    According to Django docs you can listen to the m2m_changed signal, which will trigger pre_add and post_add actions.

    Using add() with a many-to-many relationship, however, will not call any save() methods (the bulk argument doesn’t exist), but rather create the relationships using QuerySet.bulk_create(). If you need to execute some custom logic when a relationship is created, listen to the m2m_changed signal, which will trigger pre_add and post_add actions.

提交回复
热议问题