我創建了一個表單並將外鍵「藝術家」設置爲CharField,以便用戶可以自由輸入藝術家的姓名如果它不存在,它將被創建。在藝術家現場輸入數據時,我收到以下錯誤:Django ValueError無法指定「'測試'」:「Profile.artist」必須是「藝術家」實例
ValueError: Cannot assign "'testing'": "Profile.artist" must be a "Artist" instance
這裏是我的models.py:
class Artist (models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class Genre (models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class Profile (models.Model):
artist = models.ForeignKey(Artist, on_delete=models.CASCADE)
genre = models.ForeignKey(Genre, on_delete=models.CASCADE)
title = models.CharField(max_length=100)
mix = models.CharField(max_length=100, blank=True)
def __str__(self):
return self.title
這裏是我的views.py:
def profile_create(request):
if request.method == 'POST':
form = ProfileForm(request.POST)
if form.is_valid():
instance = form.save(commit=False)
instance.user = request.user
instance.save()
form = ProfileForm(None) #clears the form for new submission
context = {
"form": form,
"instance": instance,
}
return render(request, "profile_form.html", context)
else:
form = ProfileForm(None)
context = {
"form": form,
}
return render(request, "profile_form.html", context)
這裏是我的forms.py:
from django import forms
from .models import Profile, Artist
class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
fields = [
"artist",
"title",
"mix",
"genre",
]
artist = forms.CharField(widget=forms.TextInput)
def clean_artist(self, commit=True):
artist = self.cleaned_data.get("artist")
if not artist:
raise forms.ValidationError("Artist is a required field.")
else:
artist, created = Artist.objects.get_or_create(name=artist)
self.cleaned_data['artist'] = artist
return super(ProfileForm, self).clean()
你當然不應該在'clean_artist'方法內調用超級'clean()'。該方法也不需要'commit'參數。 –
'commit = True'參數不需要 – Sayse