不同的ContentType的CharField选择

时间:2019-01-25 15:47:33

标签: django database python-3.x django-models

假设我有这个模型:

class TaggedItem(models.Model):
    tag = models.SlugField()
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')

    sub_type = models.CharField(choices=CHOICES)

CHOICES = [(a, A),(b, B), ...]

有没有一种方法可以根据所涉及的ContentType排除某些选择?

1 个答案:

答案 0 :(得分:1)

覆盖save方法可能是最好的方法(其他信号),请参阅相关文档here。 尝试类似的东西:

def save(self, *args, **kwargs):
    if self.content_type: # your logic
    self.CHOICES = restricted_choices
    super().save(*args, **kwargs)
相关问题