可能重複:
Django 「Enter a list of values」 form error when rendering a ManyToManyField as a Textarea試圖挽救ManyToManyField
我python,django,ajax
這些在artist
輸入字段中的數據。我得到了Enter a list of values.
錯誤。你能幫我保存這些數據嗎?感謝
型號
artist = models.ManyToManyField(ApiArtist, blank=True)
形式和驗證
class ApiSongForm(ModelForm):
class Meta:
model = ApiSong
widgets = {
'artist': forms.TextInput(),
}
def clean_artist(self):
data = self.cleaned_data
artist_list = data.get('artist', None)
if artist_list is not None:
for artist_name in artist_list.split(','):
artist = ApiArtist(name=artist_name).save()
return artist_list
編輯
現在我已經改變了從提供的鏈接代碼複製/粘貼。但是,我得到Cannot resolve keyword 'artist' into field. Choices are: apisong, id, name
。錯誤信息。 這是我的ApiArtist and SongModel。謝謝
class ModelCommaSeparatedChoiceField(ModelMultipleChoiceField):
widget = forms.TextInput
def clean(self, value):
if value is not None:
print value
value = [item.strip() for item in value.split(",")] # remove padding
return super(ModelCommaSeparatedChoiceField, self).clean(value)
class ApiSongForm(ModelForm):
artist = ModelCommaSeparatedChoiceField(
required=False, queryset=ApiArtist.objects.filter(), to_field_name='artist')
class Meta:
model = ApiSong
請包括你的錯誤的追蹤。 – jdi
@jdi我剛剛得到'輸入值列表'。我沒有得到任何回溯。 – Kulbir
@jdi感謝您的鏈接。 :-) – Kulbir