abstract没有列名

时间:2018-05-12 04:08:44

标签: python django

我使用Django2.0.5 + Python3.6。我定义了两个类:

class Person(models.Model):
    isMale = models.BooleanField(default=True)
    name = models.CharField(max_length=32)
    age = models.IntegerField()
    mail = models.CharField(max_length=64)
    phone = models.CharField(max_length=32)
    class Meta:
        abstract = True

    def __str(self):
        return self.name


class User(Person):
    username = models.CharField(max_length=64)
    password = models.CharField(max_length=64)
    department = models.CharField(max_length=64)
    date_joined = models.DateField()

    class Meta:
        abstract = False
    def __str(self):
        return self.department + "'s " + self.name

然后,我跑

   python manage.py makemigrations
   python manage.py migrate 

虽然是sqlmigrate命令,但我可以看到:

migrations.CreateModel(
            name='User',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('isMale', models.BooleanField(default=True)),
                ('name', models.CharField(max_length=32)),
                ('age', models.IntegerField()),
                ('mail', models.CharField(max_length=64)),
                ('phone', models.CharField(max_length=32)),
                ('address', models.CharField(max_length=32)),
                ('username', models.CharField(max_length=64)),
                ('password', models.CharField(max_length=64)),
                ('department', models.CharField(max_length=64)),
                ('date_joined', models.DateField()),
            ],
            options={
                'abstract': False,
            },
        ),


class Meta:
    abstract = True

但是在shell中,如果我运行这样的命令:

p = User(username="",password="",department="",isMale=True,name="",age=1,mail="",phone="",date_joined=timezone.now())
 p.save()

我明白了: django.db.utils.OperationalError:表vsoa_user没有名为isMale的列

。但是在sqlmigrate命令中,我可以" isMale" "用户"中的字段。如果我删除" abstract = True"来自Person,此错误消失。

我读过https://docs.djangoproject.com/en/2.0/topics/db/models/,but没有用。

1 个答案:

答案 0 :(得分:0)

伙计,我尝试使用相同的代码,它对我有用。我从管理员添加了一个对象并添加了它。我唯一改变的是usera的用户名​​,因为我已经有了用户模型。也许你在代码中导入auth.User并导致冲突?

class Person(models.Model):
    isMale = models.BooleanField(default=True)
    name = models.CharField(max_length=32)
    age = models.IntegerField()
    mail = models.CharField(max_length=64)
    phone = models.CharField(max_length=32)
    class Meta:
        abstract = True

    def __str__(self):
        return self.name


class Usera(Person):
    username = models.CharField(max_length=64)
    password = models.CharField(max_length=64)
    department = models.CharField(max_length=64)
    date_joined = models.DateField()

    class Meta:
        abstract = False
    def __str__(self):
        return self.department + "'s " + self.name

from django.contrib import admin
from .models import *
# Register your models here.

admin.site.register(Usera)