【发布时间】:2020-09-10 09:34:37
【问题描述】:
我正在学习 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);
【问题讨论】:
-
你应该写
name =,而不是name :(适用于所有字段)。
标签: python django python-3.x django-models mysql-python