我在输入字段中有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 和 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