1

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?

ELsssss
  • 216
  • 2
  • 11
  • please send your whole model – vorujack Oct 24 '18 at 10:50
  • Please include the full traceback in your question. It will show where the error is coming from. – Alasdair Oct 24 '18 at 10:51
  • Just in case, as you've added model fields, you're getting error for `Receipt` model, not `Book`. Either you just missing it you you're having non-described relations – Slam Oct 24 '18 at 11:09
  • The traceback is telling you that the error is occurring in `ReceiptForm`, so you should include that in your question. – Alasdair Oct 24 '18 at 11:51

2 Answers2

1

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:

Arman Ordookhani
  • 6,031
  • 28
  • 41
0

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

Slam
  • 8,112
  • 1
  • 36
  • 44
  • I want to get the last modified date time after the update operation is done. I got error too after i add in the default value. – ELsssss Oct 24 '18 at 10:59
  • This looks like you're using model out of sync with your code. Please edit your question with complete code to reproduce this issue – Slam Oct 24 '18 at 11:01