我有以下型号:
class Item(models.Model): # fields # ... class Collection(models.Model): items = models.ManyToManyField(Item,related_name="collections") # other fields # ...
现在我想要两件事:
>我想控制是否可以将一个Item添加到Collection中.
>如果添加或删除了Item,我希望Collection更新其某些字段.
对于第二个问题,我知道有一个django.db.models.signals.m2m_changed
可以用来挂钩关系的变化.是否允许/确定在信号回调中更改Collection?我是否也可以使用该信号“中止”问题1的插入?
解决方法
我认为处理所需行为的最佳方法不是使用信号,而是通过直通表上的重写的save()和delete()方法,您可以使用参数通过参见:
https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ManyToManyField.through显式定义,并且:
https://docs.djangoproject.com/en/dev/topics/db/models/#overriding-predefined-model-methods
像这样的东西:
# -*- coding: utf-8 -*- from django.db import models class Item(models.Model): # fields # ... class Collection(models.Model): items = models.ManyToManyField(Item,related_name="collections",through="CollectionItem") # other fields # ... class CollectionItem(models.Model): collection = models.ForeignKey(Collection) item = models.ForeignKey(Item) def save(self,*args,**kwargs): # Only allow this relationship to be created on some_condition # Part 1 of your question. if some_condition: super(CollectionItem,self).save(*args,**kwargs) # Update some fields on Collection when this # relationship is created # Part 2 of your question (1/2) self.Collection.updateSomeFields() def delete(self,**kwargs): collection = self.collection super(CollectionItem,self).delete(*args,**kwargs) # Update some fields on Collection when this relationship # is destroyed. # Part 2 of your question (2/2) collection.updateSomeFields()
顺便提一下,你会发现添加一个关系会在这个模型上产生一个保存信号.
而且,关于信号,一旦你有了直通表,你就可以监听pre_save和/或post_save信号,但是它们都不允许你直接否决关系的创建.
如果您的一个或两个模型由第三方提供,而您实际上无法创建直通表,那么,是的,信号路径可能是唯一的出路.
https://docs.djangoproject.com/en/dev/ref/signals/#m2m-changed
在这种情况下,您可以侦听m2m_changed事件并触发对集合对象的更新(问题的第2部分),并追溯删除不恰当创建的关系(问题的第1部分).然而,由于后一位是一个非常丑陋的kludgy,如果可以,我会坚持使用显式通过表.
原文地址:https://www.jb51.cc/python/186451.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。