2๐
โ
You can actually use model-level validation:
class Survey(models.Model):
survey_type = models.CharField(max_length=1, choices=SURVEY_TYPES)
def __init__(self, *args, **kwargs):
super().__init__(self, *args, **kwargs)
self._old_survey_type = self.survey_type
def clean(self):
if (self.survey_type != self._old_survey_type) \
and survey_typeself.response_set.exists():
raise ValidationError('Cannot modify the type of a started survey')
Be careful though, Model.clean
is not automatically called when you save an object. It does when when a ModelForm
gets validated (hence also in admin), but otherwise you have to check if it does or call it yourself.
๐คIvan
0๐
set survey
and response
field in Response
model unique by
unique together like this:
class Response(models.Model):
survey = models.ForeignKey(Survey)
response = models.TextField()
class Meta:
unique_together = (("survey", "response"),)
๐คHasan Ramezani
Source:stackexchange.com