반응형
- 모델에 DateTimeField 추가
-
create_at = models.DateTimeField(default=timezone.now())
-
- makemigrations를 이용하여 DB Schema 생성 준비
- 이 때 기존에 row가 있기 때문에 이를 어떤 값으로 채울지 결정하라고 한다.
- timezone.now를 입력하여 현재 시간으로 기존 로우에 대한 값을 설정한다.
-
$ python manage.py makemigrations System check identified some issues: WARNINGS: HelloWorld.Post.create_at: (fields.W161) Fixed default value provided. HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use `django.utils.timezone.now` You are trying to add a non-nullable field 'create_at' to comment without a default; we can't do that (the database needs something to populate existing rows). Please select a fix: 1) Provide a one-off default now (will be set on all existing rows with a null value for this column) 2) Quit, and let me add a default in models.py Select an option: 1 Please enter the default value now, as valid Python The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now Type 'exit' to exit this prompt >>> timezone.now Migrations for 'HelloWorld': HelloWorld/migrations/0003_auto_20220210_1024.py - Add field create_at to comment - Add field create_at to post - Alter field post_contents on post
- DB에 반영한다.
-
$ python manage.py migrate System check identified some issues: WARNINGS: HelloWorld.Post.create_at: (fields.W161) Fixed default value provided. HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use `django.utils.timezone.now` Operations to perform: Apply all migrations: HelloWorld, admin, auth, contenttypes, sessions Running migrations: Applying HelloWorld.0003_auto_20220210_1024... OK
-
반응형
'Django' 카테고리의 다른 글
Django view에서 넘기지 않은 key 값을 구분하기 (0) | 2022.02.13 |
---|---|
Django 주석 달기 (0) | 2022.02.13 |
Queryset 역순으로 정렬하기 (How to reverse the order of query result) (0) | 2022.02.09 |
Django ForeignKey 데이터 가져오기 (0) | 2022.02.09 |
Django Models, Create Table, ForeignKey (0) | 2022.02.09 |