Django Mysql 不迁移模型
Django Mysql not migrating models
我正在学习Django,我似乎无法迁移模型。唯一正在迁移的是 ID
,尽管模型有 name
、description
、image
等
models.py
from django.db import models
# Create your models here.
class Destination(models.Model):
name: models.CharField(max_length=100)
price: models.IntegerField()
desc: models.TextField()
img: models.ImageField(upload_to='pics')
special: models.BooleanField(default=False)
生成0001_initial.py
# Generated by Django 3.0.6 on 2020-05-23 14:00
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Destination',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
],
),
]
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'python',
'USER': 'root',
'PASSWORD': '1q2w3e4r',
'HOST': 'localhost',
'PORT': '3306',
}
}
迁移输出
$ python3 manage.py sqlmigrate travel 0001
--
-- Create model Destination
--
CREATE TABLE `travel_destination` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY);
Class 属性是用等式符号 (=
) 定义的, 而不是 用冒号 (:
) 定义的。所以你写模型:
class Destination(models.Model):
# = instead of :
name <b>=</b> models.CharField(max_length=100)
price <b>=</b> models.IntegerField()
desc <b>=</b> models.TextField()
img <b>=</b> models.ImageField(upload_to='pics')
special <b>=</b> models.BooleanField(default=False)
您的模型应如下所示
class Destination(models.Model):
name = models.CharField(max_length=100)
price = models.IntegerField()
desc = models.TextField()
img = models.ImageField(upload_to='pics')
special = models.BooleanField(default=False)
我正在学习Django,我似乎无法迁移模型。唯一正在迁移的是 ID
,尽管模型有 name
、description
、image
等
models.py
from django.db import models
# Create your models here.
class Destination(models.Model):
name: models.CharField(max_length=100)
price: models.IntegerField()
desc: models.TextField()
img: models.ImageField(upload_to='pics')
special: models.BooleanField(default=False)
生成0001_initial.py
# Generated by Django 3.0.6 on 2020-05-23 14:00
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Destination',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
],
),
]
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'python',
'USER': 'root',
'PASSWORD': '1q2w3e4r',
'HOST': 'localhost',
'PORT': '3306',
}
}
迁移输出
$ python3 manage.py sqlmigrate travel 0001
--
-- Create model Destination
--
CREATE TABLE `travel_destination` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY);
Class 属性是用等式符号 (=
) 定义的, 而不是 用冒号 (:
) 定义的。所以你写模型:
class Destination(models.Model):
# = instead of :
name <b>=</b> models.CharField(max_length=100)
price <b>=</b> models.IntegerField()
desc <b>=</b> models.TextField()
img <b>=</b> models.ImageField(upload_to='pics')
special <b>=</b> models.BooleanField(default=False)
您的模型应如下所示
class Destination(models.Model):
name = models.CharField(max_length=100)
price = models.IntegerField()
desc = models.TextField()
img = models.ImageField(upload_to='pics')
special = models.BooleanField(default=False)