如何在Django中自动更改模型中的字段选项?

时间:2015-04-10 06:16:05

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

如何在特定条件下更改模型的字段选项? 例如,我想通过is_it_question函数将False(可编辑)的选项更改为save

class TypeFourChoice(models.Model):
    question_choice = models.ForeignKey(TypeFour)
    is_it_question = models.BooleanField(default=False)
    number = models.PositiveIntegerField(blank=False, default=100, editable=False)
    word_or_words = models.CharField(default='', blank=False, max_length=20)
    timestamp = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return "{} : {}".format(self.question_choice, self.word_or_words)

    def save(self, *args, **kwargs):
        if self.pk is None and self.is_it_question:
            self.number = TypeFourChoice.objects.filter(is_it_question=True).count() + 1
            ؟؟؟؟***self.is_it_question.editable = False***????
        super(TypeFourChoice, self).save(*args, **kwargs)

1 个答案:

答案 0 :(得分:0)

您正在寻找此声明

self._meta.get_field_by_name('is_it_question')[0].editable = False

但你必须注意这一行动的后果,请阅读post