python - Setting form data in clean_field method -
suppose there's model:
class somemodel(models.model): content = models.filefield(...) number_of_lines = models.integerfield()
i use modelform
, content
field validated in client_content()
method. count number of lines in content
, want use number set field's number_of_lines
value:
what came is:
class someform(forms.modelform): def clean_content(self): self.number_of_lines = 0 # validation self.number_of_lines +=1 # ... def save(self, commit=true): instance = super().save(commit=true) instance.number_of_lines = self.number_of_lines if commit: instance.save() return instance
but self.number_of_lines
thing not right. there better way? maybe it's possible set self.cleaned_data['number_of_accounts']
in clean_content
method?
usually people suggest use clean()
method that, in case have remove line counter clean_content
, count lines inside clean()
.
Comments
Post a Comment