Search code examples
pythondjangodjango-admindjango-managers

Django Admin Manager Override


I have a Multiple Choice Questions, in which Model Question is Question and Choices as answers. I want to limit the number of Choices that can be created to a Question to 4.

A models.Manager is used to verify the number of choices for the question.

class Question(models.Model):
    QUESTION_TYPES = (
    ('MC', 'Multiple Choice'),
    ('SB', 'Subjective'),
    )
    question_type = models.CharField(choices=QUESTION_TYPES, max_length=2, default='MC')
    question_text = models.TextField(null=False, blank=False)

and Choice

class Choice(models.Model):
    choice_text = models.CharField(max_length=100, null=True)
    question= models.ForeignKey(Question, null=True , related_name='choices')
    is_answer = models.BooleanField(default=False)
    objects = ChoiceManager()

Custom Manager

class ChoiceManager(models.Manager):
    def create(self, **kwargs):
        question = kwargs.pop('question',None)
        if question is not None:
            if question.choices.all().count() > 4:    # see related_name 
                raise AssertionError
            else:
                return self

Everything works fine if I am using python shell to create Model Instances.

BUT: When I am using the AdminSite. I am able to create more than 4 Choices to a question. How do I attain my desired behavior at AdminSite (Raise Error at Admin Site)? Override the Manager at Admin Level? How would I proceed that?

admin.site.register(Question)
admin.site.register(Choice) 

Solution

  • Needed To override the save method in the model class itself.

    class Choice(models.Model):
        choice_text = models.CharField(max_length=100, null=True)
        question= models.ForeignKey(Question, null=True , related_name='choices')
        is_answer = models.BooleanField(default=False)
        objects = ChoiceManager()
    
        def save(self, *args, **kwargs):
            if self.question.choices.all().count() > 4:
                print "You Shall Not Save"
                raise ValueError
            else:
                print "Super Method Called"
                super(Choice, self).save(*args, **kwargs)