Django ModelChoiceField和date:match = date_re.match(value)

时间:2015-09-08 13:02:42

标签: python django date django-queryset

我有一个表单,其中包含我要保存日期的其他内容,该日期是从其他模型动态生成的,其中存储了所有可用日期。

尝试保存日期时出现问题:我收到错误" TypeError at / form / expected string或buffer"。

我无法理解,因为在" POST"我得到字符串和模型的对象表示也是字符串:

我的模特:

class OrderPickUp(models.Model):
    name = models.CharField(max_length=128)
    pick_up_date = models.DateField(auto_now=False, auto_now_add=False)

class AvailableDates(models.Model):
    city_A = models.DateField(auto_now=False, auto_now_add=False)

    def __str__(self):
        return str(self.city_A)

froms.py

class OrderPickUpForm(forms.ModelForm):
    def __init__ (self, *args, **kwargs):
        super(OrderPickUpForm, self).__init__(*args, **kwargs)
        self.fields["pick_up_date"] = forms.ModelChoiceField(queryset=AvailableDates.objects.all())

    class Meta:
        model = OrderPickUp
        fields = ['name', 'pick_up_date']

views.py

def pick_up(request):
    form = OrderPickUpForm()
    if  request.POST:
        form = OrderPickUpForm(request.POST)
        if form.is_valid():
            form.save()
            context = {
            "title":"thanks for ordering",
            }
        else: 
            context = {
            "title":"Something missing",
            }
    else:
        context = {
            "form": form,
        }
    return render(request, "form.html", context)

但是当我提交表单时,我收到错误:

Traceback:
File "/Users/marius/.venvs/plays/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/marius/developer/TUTORIALS/PLAYS/testavimai/datos/views.py" in pick_up
  12.         if form.is_valid():
File "/Users/marius/.venvs/plays/lib/python3.4/site-packages/django/forms/forms.py" in is_valid
  184.         return self.is_bound and not self.errors
File "/Users/marius/.venvs/plays/lib/python3.4/site-packages/django/forms/forms.py" in errors
  176.             self.full_clean()
File "/Users/marius/.venvs/plays/lib/python3.4/site-packages/django/forms/forms.py" in full_clean
  394.         self._post_clean()
File "/Users/marius/.venvs/plays/lib/python3.4/site-packages/django/forms/models.py" in _post_clean
  430.             self.instance.full_clean(exclude=exclude, validate_unique=False)
File "/Users/marius/.venvs/plays/lib/python3.4/site-packages/django/db/models/base.py" in full_clean
  1149.             self.clean_fields(exclude=exclude)
File "/Users/marius/.venvs/plays/lib/python3.4/site-packages/django/db/models/base.py" in clean_fields
  1191.                 setattr(self, f.attname, f.clean(raw_value, self))
File "/Users/marius/.venvs/plays/lib/python3.4/site-packages/django/db/models/fields/__init__.py" in clean
  587.         value = self.to_python(value)
File "/Users/marius/.venvs/plays/lib/python3.4/site-packages/django/db/models/fields/__init__.py" in to_python
  1274.             parsed = parse_date(value)
File "/Users/marius/.venvs/plays/lib/python3.4/site-packages/django/utils/dateparse.py" in parse_date
  60.     match = date_re.match(value)

Exception Type: TypeError at /form/
Exception Value: expected string or buffer

任何想法??

1 个答案:

答案 0 :(得分:0)

我认为您在表单和模型定义上存在差异。您已将pick_up_date模型中的OrderPickUp定义为DateField,但在表单中,您将其设为ModelChoiceField,用于外键类型的字段。如果您想要从pick_up_date列表中进行选择,则应将模型修改为:

class OrderPickUp(models.Model):
    name = models.CharField(max_length=128)
    pick_up_date = models.ForeighKey('AvailableDates')

请注意,最好使用单数名称而不是复数,因此您可能希望将AvailableDates更改为AvailableDate