John Doe
John Doe

Reputation: 1639

CharField choices for different ContentType

Suppose I have this model :

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)

and

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

is there a way to exclude some choices depending on the ContentType involved ?

Upvotes: 0

Views: 113

Answers (1)

May.D
May.D

Reputation: 1910

Overriding the save method is probably the best way to go (other signals), see related doc here. Try something like:

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

Upvotes: 1

Related Questions