0
這是我的樣品模型:如何將modelchoicefield轉換爲django admin中的charfield?
class Booking(models.Model):
email = models.EmailField(unique=True)
first_name = models.CharField(max_length=256)
last_name = models.CharField(max_length=256)
class MailList(models.Model):
email = models.EmailField(unique=True)
booking = models.ForeignKey(Booking, blank=True, null=True)
我有一個表格,我已經修改了FK領域成爲一個CharField。 我的代碼去:
form.py
class MailListAdminForm(forms.ModelForm):
booking= forms.CharField(required=False)
class Meta:
model = MailList
def clean_myfield(self):
data = self.cleaned_data['booking']
try:
self.booking= Booking.objects.get(pk=data)
return data
except (KeyError, Booking.DoesNotExist):
raise forms.ValidationError('Invalid Booking ID. Please try again.')
admin.py
class MailListAdmin(admin.ModelAdmin):
form = mailListAdminForm
我得到一個錯誤,「Django的版本:1.3.1
異常類型:ValueError異常
無法指定「u'143590」「:」MailList.booking「必須是」預訂「inst ANCE「。
任何想法如何解決這個問題?由於
mailListAdminForm應該是MailListAdminForm? – lakesh