Title seems complicated but its not really.
Lets say I have 2 models:
First is Choices that contains a title and boolean to see if the choice is correct or not.
class Choice(models.Model):
content = models.CharField(max_length=200, default=None)
correct = models.BooleanField(default=False)
Second is Check that checks if the use has chosen the correct choice:
class CheckQuestion(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
selected_choice = models.ForeignKey(Choice, on_delete=models.CASCADE)
correct = models.BooleanField(default=None)
The correct field must be equal to the correct field of the selected choice. So basically:
correct = selected_choice.correct
I have tried implementing these using @property like this:
@property
def correct(self):
if self.selected_choice.correct:
correct = True
else:
correct = False
return correct
And I tried save like this:
def save(self,*args,**kwargs):
self.correct = self.selected_choice.correct
super(CheckQuestion,self).save(*args,**kwargs)
But neither worked, property method was giving giving a "cannot be null" error and save method just always returned false.
What else can I do?