这是我的样本模型:
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”必须是“预订”实例。“
任何想法如何解决这个问题?感谢
答案 0 :(得分:0)
如果我理解你的问题
然后您收到此错误,因为
在您的观看中,您尝试使用MailList.booking
属性预订是FK到表预订
您无法存储必须传递Booking表对象的值
代表MailList.booking
`MailList.booking` = bookingobject
bookingobject
应该是您的预订表的实例