본문 바로가기

오늘의 오류들/Django

Django, DateTimeField(auto_now_add=True) not working

[개발환경:ubuntu20.04 lts]


 

장고에서 모델을 만들고 마이그레이션을 하던 도중 모델 필드 오류가 발생해서 포스팅합니다.

 


 

https://stackoverflow.com/questions/38746786/django-datetimefieldauto-now-add-true-not-working

 

Django, DateTimeField(auto_now_add=True) not working

I am a newbie of Django. This is the problem I have encountered. models.py: created_time = models.DateTimeField('Created Time', auto_now_add=True) When I migrations: Then, I add the default to ...

stackoverflow.com

 스택오버필드의 답변을 보니 models의 DateField  필드는 파라미터값으로 null=True와 객체 이름을 써주면 된다고 합니다. 

 

last_updated =  models.DateField('last_updated',auto_now_add=True,null=True)

이렇게 변경하니 마이그레이션 준비가 잘 진행됩니다.

 


*의문점

그런데 Post모델에서 created_at 필드와 updated_at필드는  방금 한것처럼 안했는데 왜 마이그레이션이

진행되었지??


[참고 사이트]

https://developer.mozilla.org/ko/docs/Learn/Server-side/Django/Models

 

Django Tutorial Part 3: Using models

이 문서에서는 LocalLibrary 웹사이트의 모델을 어떻게 정의할지 보여줄 것입니다. 모델이라는 것이 무엇인지, 어떻게 선언하는지, 그리고 주된 필드 타입들에 대해서 설명합니다. 그리고 모델의

developer.mozilla.org

https://docs.djangoproject.com/en/3.1/ref/forms/fields/#django.forms.DateTimeField

 

Form fields | Django documentation | Django

Django The web framework for perfectionists with deadlines. Overview Download Documentation News Community Code Issues About ♥ Donate

docs.djangoproject.com