Django模型:普通祖先继承&移民 [英] Django Models: Common Ancestor Inheritance & Migration

查看:116
本文介绍了Django模型:普通祖先继承&移民的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以为我会通过开发一个大型的商业应用程序来玩耍,用Django来呃我的python游戏。我看到需要一个共同的祖先方法来建模遗传,并尝试基于官方文档。但是,我不断得到这个非常讨厌的消息,我不知道该怎么做。

I thought I would up my python game with Django a bit by developing a large scale business app for fun. I seen the need for a common ancestor approach to model inheritence and tried to implement it based on the official documentation. However, I keep getting this very annoying Message which I'm not sure what to do with.


  • Dj版本: Django 1.7

  • Python 3.4.2

  • Dj Version: Django 1.7
  • Py Version: Python 3.4.2

留言

$ python manage.py makemigrations




You are trying to add a non-nullable field 'businessentity_ptr' to business without a default; we can't do that (the database needs something to populate existing rows).

Please select a fix:
 1) Provide a one-off default now (will be set on all existing rows)
 2) Quit, and let me add a default in models.py


Models.py

class BusinessEntity(models.Model):
    title = models.CharField(max_length=180)

    def __str__(self):
        return self.title


class Business(BusinessEntity):
    description = models.TextField(max_length=600)
    claimed = models.BooleanField(default=False)
    slug = models.SlugField()
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated = models.DateTimeField(auto_now_add=False, auto_now=True)

    def __str__(self):
        return self.description

我试过的(每个人都会讨厌):

What I've Tried, (which everyone will hate):


  1. 删除数据库&安培;重新迁移

  2. 为所有字段设置默认值

  3. 将所有字段设置为 null = True

  1. Deleting the DB & Re-migrating
  2. setting a default value for all fields
  3. Setting all fields to null = True

我已经看到一个黑客,但我不认为这是一个好办法。也许有人在那里了解Django常见的祖先更好,并指出我的方向正确。

I have seen a hack around for this but I don't think it's a good approach. Maybe there is someone out there who understand Django Common Ancestors much better and point me in the right direction.

推荐答案

由于您的父母模式旨在抽象,您应该将其标记为这样。

Since your parent model is intended to be abstract, you should mark it as such.

class BusinessEntity(models.Model):
    title = models.CharField(max_length=180)

    class Meta:
        abstract = True

这样可以防止Django为它创建一个单独的表,因此需要一个 _ptr 字段从子类指向它。相反,您的子类的表将被创建为直接包含继承的字段。

This prevents Django from creating a separate table for it, and therefore needing a _ptr field to point back to it from the subclass. Instead, the table for your subclass will be created to include the inherited field(s) directly.

这篇关于Django模型:普通祖先继承&移民的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆