Django ModelForm with extra fields that are not in the model

后端 未结 7 409
甜味超标
甜味超标 2021-01-31 02:00

I have done a ModelForm adding some extra fields that are not in the model. I use these fields for some calcualtions when saving the form.

The extra fie

7条回答
  •  再見小時候
    2021-01-31 02:44

    This answer may be too late for the original poster, but I thought it might help others. I had the same problem and I did notice that self.cleaned_data('artist_id') can be accessed in the clean() method, but not in the clean_artist().

    When I added the extra fields in the 'fields' declaration of Meta, then it worked.

        class Meta:
            model = Music
            fields=[..., 'artist_id']
    

    You should be able to access the self.cleaned_data('artist_id') in clean_artist().

提交回复
热议问题