Django-Manytomy关系中的过滤

2024-05-16 09:53:57 发布

您现在位置:Python中文网/ 问答频道 /正文

想象一下运营商,数据,语音的模式。数据计划和语音计划包含外键(载波)。现在,由于有些数据计划只能用于某些语音计划,数据计划模型包含许多字段(语音计划)。但是,它列出了所有可用运营商的所有语音计划(在Django admin中)。如何过滤同一运营商的语音计划?你知道吗

class Carrier(models.Model):
   # bunch of attributes
   #...

class Voice_plan(models.Model):
   # bunch of attributes
   carrier = ForeignKey(Carrier)
   #...

class Data_plan(models.Model):
   ...
   carrier = ForeignKey(Carrier)
   # if not available for all voice plans
   for_voice_plans = ManyToManyField(Voice_plan) # should be for those voice plans that share the same carrier...

Tags: of数据formodelmodels语音计划class