以下是我的模型
class Seminar(models.Model):
seminarID = models.AutoField(primary_key=True)
presenter_name = models.CharField(max_length=200)
location_name = models.TextField()
seminar_DT = models.DateTimeField(default=datetime.datetime.now)
capacity = models.IntegerField(default=50)
class Registration(models.Model):
registration_ID = models.AutoField(primary_key=True)
seminar=models.ForeignKey(Seminar,to_field='seminarID',on_delete=models.CASCADE)
attendee_name = models.CharField(max_length=200,null=True)
email_address = models.EmailField()
email_sent = models.BooleanField(default=False)
这是我为注册模型创建的 ModelForm
class RegistrationForm(forms.ModelForm):
class Meta:
model = Registration
widgets = {
"registration_ID": forms.NumberInput(attrs={'required': "required"}),
"seminar": forms.SelectMultiple(attrs={'required': "required"}),
"attendee_name": forms.TextInput(attrs={'required': "required"}),
"email_address": forms.TextInput(attrs={'required': "required"}),
"email_sent": forms.NullBooleanSelect(attrs={'required': "required"}),
}
fields = ('seminar','attendee_name','email_address','email_sent')
当我在我的模板上运行上面的 RegistrationForm 时,我得到以下窗口
研讨会字段,在注册模型中定义为ForeignKey,在研讨会模型中指的是seminarID。研讨会出现在预先填充的注册表单中,这很好,但它显示为研讨会对象(2),研讨会对象(3)......研讨会模型中的研讨会ID也是AutoField和Primarykey。
当我保存 RegistrationForm 时,我的 postgresql 数据库不会使用新值和选定的研讨会对象 (*) 进行更新。请指教