django抽象模型与常规遗传 [英] django abstract models versus regular inheritance
问题描述
除了语法,使用django抽象模型和使用简单的Python继承与django模型有什么区别?优点和缺点?
更新:我认为我的问题被误解,我收到了一个抽象模型和继承自django.db.models的类之间的区别的回应。模型。 我实际上想知道从django抽象类(Meta:abstract = True)继承的模型类与继承自object(而不是models.Model)的普通Python类之间的区别。 / strong>
这是一个例子:
class User对象):
first_name = models.CharField(..
def get_username(self):
return self.username
class User模型):
first_name = models.CharField(...
def get_username(self):
return self.username
class Meta:
abstract = True
class员工(用户):
title = models.CharField(...
我真的想知道
从django抽象继承的模型类之间的区别class(Meta:abstract = True)和一个
纯Python类,继承自say,'object'(和n
models.Model)。
Django将只生成 models的子类的表。模型
,所以前者...
class User(models.Model):
first_name = models.CharField(max_length = 255)
def get_username(self):
return self.username
class Meta:
abstract = True
class员工(用户):
title = models.CharField(max_length = 255)
...将导致生成一个表,沿...的行...
CREATE TABLE myapp_employee
(
id INT NOT NULL AUTO_INCREMENT,
first_name VARCHAR(255)NOT NULL,
title VARCHAR(255)NOT NULL,
PRIMARY KEY )
);
...而后者...
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ b $ get $ return self.username
class Employee(User):
title = models.CharField(max_length = 255)
...不会导致生成任何表。
您可以使用多重继承来执行此操作...
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
类中定义的字段,因此最终会出现如下表格...
CREATE TABLE myapp_employee
(
id INT NOT NULL AUTO_INCREMENT,
title VARCHAR(255)NOT NULL,
PRIMARY KEY(id)
);
Besides the syntax, what's the difference between using a django abstract model and using plain Python inheritance with django models? Pros and cons?
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).
Here is an example:
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(...
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 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)
);
...whereas the latter...
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)
...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屋!