Django Mysql不迁移模型

我正在学习Django,但似乎无法迁移模型,唯一要迁移的就是ID。尽管模型具有名称,描述,图像等

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 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)
点赞
评论
fharum
fharum

Class attributes are defined with an equation sign (=), not with a colon (:). So you write the models with:

class Destination(models.Model):
    # = instead of :
    name = models.CharField(max_length=100)
    price = models.IntegerField()
    desc = models.TextField()
    img = models.ImageField(upload_to='pics')
    special = models.BooleanField(default=False)
点赞
评论