django adds or removes instances of fields in the original table

  • 2020-10-23 20:09:25
  • OfStack

1. If models. py file:


timestamp = models.DateTimeField(' Save the date ')

You will be prompted:


(env8) D:\Desktop\env8\Scripts\mysite>python manage.py makemigrations
You are trying to add a non-nullable field 'timestamp' to article without a defa
ult; 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)
 2) Quit, and let me add a default in models.py

Enter Select an option: 1 (this requires you to set the default value of the new field, which will be added to the new field as well)


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

It's hard to fix this

can


(env8) D:\Desktop\env8\Scripts\mysite>python manage.py shell
(env8) D:\Desktop\env8\Scripts\mysite>from django.db import connection
(env8) D:\Desktop\env8\Scripts\mysite>cursor=connection.cursor()
(env8) D:\Desktop\env8\Scripts\mysite>cursor.execute('ALTER TABLEArticle add column timestamp varchar(100) default 0')
 

2. If models. py file:


timestamp = models.DateTimeField(' Save the date ',default=timezone.now,blank=False, null=False)
timestamp = models.DateTimeField(' Save the date ',default=timezone.now,blank=True, null=True)

blank

When set to True, the field can be empty. When set to False, the fields must be filled in. The character fields CharField and TextField store null values as empty strings. If it is True, the field is allowed to be empty and is not allowed by default.

null

When set to True, django USES Null to store null values. The date, time, and number font fields do not accept empty strings. So set IntegerField, DateTimeField field can be empty, need to set blank, null are set to True. If it is True, the null value will be stored as NULL, which defaults to False. If you want to set BooleanField to null, you can choose the NullBooleanField field.


(env8) D:\Desktop\env8\Scripts\mysite>python manage.py makemigrations You wouldn't have the following hint 
(env8) D:\Desktop\env8\Scripts\mysite>python manage.py migrate  There you go. There's no data type in the middle (error prone) (if you want to set the default) 

3. Database design is the core of the whole website development

Supplement: timestamp = ES57en. DateTimeField(' save date ')


(env8) D:\Desktop\env8\Scripts\mysite>python manage.py makemigrations
You are trying to add a non-nullable field 'timestamp' to article without a defa
ult; 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)
 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()
>>> '2017-12-16 05:04:31.000' (Add the data type format of the field) 
Migrations for 'blog':
 0002_article_timestamp.py:
  - Add field timestamp to article
(env8) D:\Desktop\env8\Scripts\mysite>python manage.py migrate
Operations to perform:
 Synchronize unmigrated apps: staticfiles, ckeditor_uploader, messages, ckedito
r, bootstrap3
 Apply all migrations: admin, blog, contenttypes, auth, sessions
Synchronizing apps without migrations:
 Creating tables...
  Running deferred SQL...
 Installing custom SQL...
Running migrations:
 Rendering model states... DONE
 Applying blog.0002_article_timestamp...D:Desktop\env8\lib\site-packa
ges\django\db\models\fields\__init__.py:1474: RuntimeWarning: DateTimeField Arti
cle.timestamp received a naive datetime (2017-12-16 05:04:31) while time zone su
pport is active.
 RuntimeWarning)
 OK
(env8) D:\Desktop\env8\Scripts\mysite>

Related articles: