gpt4 book ai didi

python - Django:在 DateTimeField 中添加 auto_now 时出现错误

转载 作者:行者123 更新时间:2023-12-01 08:53:14 25 4
gpt4 key购买 nike

我正在尝试做 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 =True,editable=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

我应该如何解决这个错误?

最佳答案

您在回溯中看到的错误位于您的表单ReceiptForm中。带有 auto_nowDateTimeField 自动为 editable=Falseblank=True,因此不能包含在表单中,除非它是只读的。您可以删除 auto_now 并使用自定义保存方法来设置 updated_at

请参阅这些问题以获取更多信息:

关于python - Django:在 DateTimeField 中添加 auto_now 时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52967071/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com