Django:在DateTimeField中添加auto_now时出错

时间:2018-10-24 10:48:35

标签: python django

我正在尝试做django api。

在models.py

class Receipt(models.Model):
    id=models.AutoField(primary_key=True)
    name=models.CharField(max_length=100)
    created_at = models.DateTimeField(default=datetime.datetime.now(),null=True,blank=True)
    updated_at = models.DateTimeField(auto_now=True,editable=False)

如果我添加auto_now =Trueeditable=False,则会出错。这是我的错误信息。

django.core.exceptions.FieldError: 'updated_at' cannot be specified for Receipt model form as it is a non-editable field

跟踪:

Traceback (most recent call last):
  File "/home/uadmin/django/env/lib/python2.7/site-packages/django/utils/autoreload.py", line 228, in wrapper
    fn(*args, **kwargs)
  File "/home/uadmin/django/env/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 124, in inner_run
    self.check(display_num_errors=True)
  File "/home/uadmin/django/env/lib/python2.7/site-packages/django/core/management/base.py", line 359, in check
    include_deployment_checks=include_deployment_checks,
  File "/home/uadmin/django/env/lib/python2.7/site-packages/django/core/management/base.py", line 346, in _run_checks
    return checks.run_checks(**kwargs)
  File "/home/uadmin/django/env/lib/python2.7/site-packages/django/core/checks/registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/home/uadmin/django/env/lib/python2.7/site-packages/django/core/checks/urls.py", line 16, in check_url_config
    return check_resolver(resolver)
  File "/home/uadmin/django/env/lib/python2.7/site-packages/django/core/checks/urls.py", line 26, in check_resolver
    return check_method()
  File "/home/uadmin/django/env/lib/python2.7/site-packages/django/urls/resolvers.py", line 256, in check
    for pattern in self.url_patterns:
  File "/home/uadmin/django/env/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/uadmin/django/env/lib/python2.7/site-packages/django/urls/resolvers.py", line 407, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/uadmin/django/env/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/uadmin/django/env/lib/python2.7/site-packages/django/urls/resolvers.py", line 400, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/uadmin/django/project/project/urls.py", line 18, in <module>
    from apps import views
  File "/home/uadmin/django/project/apps/views.py", line 14, in <module>
    from .forms import ReceiptForm
  File "/home/uadmin/django/project/apps/forms.py", line 4, in <module>
    class ReceiptForm(ModelForm):
  File "/home/uadmin/django/env/lib/python2.7/site-packages/django/forms/models.py", line 266, in __new__
    apply_limit_choices_to=False,
  File "/home/uadmin/django/env/lib/python2.7/site-packages/django/forms/models.py", line 159, in fields_for_model
    f.name, model.__name__)
django.core.exceptions.FieldError: 'updated_at' cannot be specified for Receipt model form as it is a non-editable field

该怎么办才能解决此错误?

2 个答案:

答案 0 :(得分:1)

在回溯中可能看到的错误是表格ReceiptForm中的错误。 DateTimeFieldauto_now分别是editable=Falseblank=True,因此除非它是只读的,否则不能包含在表单中。您可以删除auto_now并使用自定义保存方法来设置updated_at

有关更多信息,请参见以下问题:

答案 1 :(得分:0)

您要实现什么?

auto_now用于为每次保存设置字段值。您无法覆盖它。

auto_now_add是在创建对象后进行的操作。

default将在不提供任何内容的情况下使用默认值。

我的猜测是您只需要default。如果没有,请描述您要解决的问题