我的一个模型有一个外键字段。正如预期的那样,此字段显示为下拉列表(选择窗口小部件)。该字段的POSTed值是一个字符串,尽管相关模型的ID具有默认类型主键(整数):
因此,使用以下方法清除数据时表单验证失败:
选择有效的选择。这个选择不是可用的选择之一。
我根本没有自定义表单定义或验证。为什么通过表单中的选择窗口小部件提供的外键未转换为正确的类型?
模特:
const getCookies = function () {
var pairs = document.cookie.split(";");
var cookiesObj = {};
for (var i = 0; i < pairs.length; i++) {
var pair = pairs[i].split("=");
cookiesObj[(pair[0] + '').trim()] = unescape(pair[1]);
}
return cookiesObj;
};
const cookies = getCookies();
观点:
class Expansion(models.Model):
created = models.DateTimeField(auto_now_add=True, verbose_name=_("Created"), help_text='created on')
modified = models.DateTimeField(auto_now=True, verbose_name=_("Modified"), help_text='Last modified')
size = models.PositiveIntegerField(verbose_name=_("Size"),
help_text=_('Specify the size in GB to increase in multiple of 50'),
validators=[validate_expansion_size])
lun = models.ForeignKey(LUN, verbose_name=_('LUN'), help_text=_('Which LUN to expand'))
user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=_('User'), help_text=_('Associate User'),
db_column='user_id')
output = models.TextField(verbose_name=_("Output"), help_text=_('The Result from the expansion'))
def __unicode__(self):
return self.lun.vdisk_uuid
class Meta:
ordering = ('-created',)
verbose_name = _('LUN Expansion')
verbose_name_plural = _('LUN Expansions')
# This model is backed by a REST endpoint, using django-rest-models
class APIMeta:
db_name = 'svc'
resource_path = 'expansions/'
resource_name = 'expansion'
resource_name_plural = 'expansions'
表格:
class ExpansionCreateView(SuccessMessageMixin, CreateView):
model = Expansion
template_name = 'expansion/create.html'
form_class = ExpansionCreateForm
success_url = reverse_lazy('expansion:index')
success_message = _("LUN successfully expanded")
def get(self, request, *args, **kwargs):
if not PSC.objects.filter(user=self.request.user).exists():
messages.warning(self.request, _("You have not set a payment method yet. Please do this first."))
return HttpResponseRedirect("{0:s}?next={1:s}".format(reverse('payment:create'), reverse('expansion:create')))
if not valid_email(self.request.user.email):
messages.warning(self.request, _("You have not set a valid email address yet. Please do this first."))
return HttpResponseRedirect("{0:s}?next={1:s}".format(reverse('account_email'), reverse('expansion:create')))
return super(ExpansionCreateView, self).get(request, *args, **kwargs)
def form_valid(self, form):
form.instance.user = self.request.user
# print(form, file=sys.stderr)
return super(ExpansionCreateView, self).form_valid(form)
模板:
class ExpansionCreateForm(forms.ModelForm):
submit_text = _('Expand LUN')
def __init__(self, *args, **kwargs):
super(ExpansionCreateForm, self).__init__(*args, **kwargs)
self.helper = FormHelper(self)
self.helper.form_id = 'id-a2goform'
self.helper.form_class = 'form-horizontal form-label-left'
self.helper.label_class = 'col-lg-2'
self.helper.field_class = 'col-lg-8'
self.helper.form_method = 'post'
self.helper.help_text_inline = True
self.helper.filter_by_widget(forms.Select).wrap(Field, css_class="select2_single ")
self.helper.filter_by_widget(forms.CheckboxInput).wrap(Field, css_class="js-switch")
self.helper.add_input(Submit('submit', self.submit_text))
class Meta:
model = Expansion
fields = ['lun', 'size']