Django Mysql不迁移模型。

问题描述 投票:1回答:2

我正在学习Django,好像无法迁移模型。唯一被迁移的是 ID虽说该模式有 name, description, image 等。

模型.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')),
            ],
        ),
    ]

设置.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);
python django python-3.x django-models mysql-python
2个回答
0
投票

类的属性是用等号来定义的(=), 用冒号(:). 所以你写的模型应该是这样的。

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)

0
投票

你的模型应该是这样的

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)
© www.soinside.com 2019 - 2024. All rights reserved.