我有以下 Django 模型:
class Video(models.Model):
# ... snip ...
# Entities detected using face tracking
faces = models.ManyToManyField(Entity, related_name="facevids", blank=True)
# Entities detected using speaker diarisation
speakers = models.ManyToManyField(Entity, related_name="speakervids", blank=True)
# Entities detected by extracting names from metadata and ocr
names = models.ManyToManyField(Entity, related_name="namevids", blank=True)
# Entities detected using logo detection
logos = models.ManyToManyField(Entity, related_name="logovids", blank=True)
class Entity(models.Model):
entity_id = models.CharField(primary_key = True, max_length = 100)
我想添加一个自定义管理器,Entity
以便我可以以与或allvids
相同的方式访问 ManyToMany 自定义字段。facevids
speakervids
到目前为止,我已经研究出如何访问和联合查询集做这样的事情
@property
def allvids(self):
return (self.facevids.get_query_set() | self.speakervids.get_query_set() |
self.namevids.get_query_set() | self.logovids.get_query_set()).distinct()
但这会返回一个查询集对象而不是一个 Manager,这意味着访问与allvids
,facevids
等不同speakervids
。我希望最终用户不需要了解实现和访问allvids
与其他 Manager 字段完全相同.