ELsssss
ELsssss

Reputation: 206

Django: Having error when add auto_now in DateTimeField

I'm trying to do django api.

In 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)

I got error if I add in auto_now =True,editable=False. Here is my error message.

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

Traceback:

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

What should I do to solve this error?

Upvotes: 1

Views: 537

Answers (2)

Arman Ordookhani
Arman Ordookhani

Reputation: 6536

The error as you could see in traceback is in you form ReceiptForm. DateTimeField with auto_now are editable=False and blank=True automatically, therefore could not be included in a form unless it's readonly. You could remove auto_now and use a custom save method to set updated_at.

See these questions for more info:

Upvotes: 1

Slam
Slam

Reputation: 8572

What you're trying to achieve?

auto_now is to set field value for every save. You can't override this.

auto_now_add is to make this once object is created.

default is to have default value to use, when you don't provide anything.

My guess is that you simply need default. If not, please describe what you're solving

Upvotes: 0

Related Questions