如何在特定条件下更改模型的字段选项?
例如,我想通过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)
答案 0 :(得分:0)