假设我有这个模型:
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排除某些选择?
答案 0 :(得分:1)
覆盖save方法可能是最好的方法(其他信号),请参阅相关文档here。 尝试类似的东西:
def save(self, *args, **kwargs):
if self.content_type: # your logic
self.CHOICES = restricted_choices
super().save(*args, **kwargs)