Django 抽象模型与常规继承 [英] django abstract models versus regular inheritance

查看:30
本文介绍了Django 抽象模型与常规继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了语法之外,使用 django 抽象模型和使用带有 django 模型的纯 Python 继承有什么区别?利弊?

Besides the syntax, what's the difference between using a django abstract model and using plain Python inheritance with django models? Pros and cons?

更新:我认为我的问题被误解了,我收到了关于抽象模型和从 django.db.models.Model 继承的类之间差异的答复.我实际上想知道从 django 抽象类 (Meta: abstract = True) 继承的模型类和从object"(而不是 models.Model)继承的普通 Python 类之间的区别.

UPDATE: I think my question was misunderstood and I received responses for the difference between an abstract model and a class that inherits from django.db.models.Model. I actually want to know the difference between a model class that inherits from a django abstract class (Meta: abstract = True) and a plain Python class that inherits from say, 'object' (and not models.Model).

这是一个例子:

class User(object):
   first_name = models.CharField(..

   def get_username(self):
       return self.username

class User(models.Model):
   first_name = models.CharField(...

   def get_username(self):
       return self.username

   class Meta:
       abstract = True

class Employee(User):
   title = models.CharField(...

推荐答案

我实际上想知道模型类之间的区别继承自 django 抽象类(元:抽象 = True)和一个继承自对象"(而不是对象")的普通 Python 类模型.模型).

I actually want to know the difference between a model class that inherits from a django abstract class (Meta: abstract = True) and a plain Python class that inherits from say, 'object' (and not models.Model).

Django 只会为 models.Model 的子类生成表,所以前者...

Django will only generate tables for subclasses of models.Model, so the former...

class User(models.Model):
   first_name = models.CharField(max_length=255)

   def get_username(self):
       return self.username

   class Meta:
       abstract = True

class Employee(User):
   title = models.CharField(max_length=255)

...将导致生成单个表,沿着...

...will cause a single table to be generated, along the lines of...

CREATE TABLE myapp_employee
(
    id         INT          NOT NULL AUTO_INCREMENT,
    first_name VARCHAR(255) NOT NULL,
    title      VARCHAR(255) NOT NULL,
    PRIMARY KEY (id)
);

...而后者...

class User(object):
   first_name = models.CharField(max_length=255)

   def get_username(self):
       return self.username

class Employee(User):
   title = models.CharField(max_length=255)

...不会导致生成任何表.

...won't cause any tables to be generated.

你可以使用多重继承来做这样的事情...

You could use multiple inheritance to do something like this...

class User(object):
   first_name = models.CharField(max_length=255)

   def get_username(self):
       return self.username

class Employee(User, models.Model):
   title = models.CharField(max_length=255)

...这将创建一个表,但它会忽略在 User 类中定义的字段,所以你最终会得到一个这样的表...

...which would create a table, but it will ignore the fields defined in the User class, so you'll end up with a table like this...

CREATE TABLE myapp_employee
(
    id         INT          NOT NULL AUTO_INCREMENT,
    title      VARCHAR(255) NOT NULL,
    PRIMARY KEY (id)
);

这篇关于Django 抽象模型与常规继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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