Django继承
Django目前支持两种不同的继承方式,包括抽象基础类和多表继承。
1、抽象基础类:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
class Author(models.Model): name=models.CharField(max_length=20) class Book(models.Model): title=models.CharField(max_length=100) num_pages=models.IntegerField() authors=models.ManyToManyField(Author) def __str__(self): return self.title class Meta(object): abstract=True class SmithBook(Book): def __init__(self,*args,**kwargs): super(self,Book).__init__(*args,**kwargs) authors=models.ManyToManyField(Author,limit_choices_to={ "name_endswith":"Smith" }) |
这里的关键是Meta嵌套类中的abstract=True,它指明了Book是一个基类。使用抽象基础类的方法,不会为基础类创建表。
在子类中的嵌套类Meta会继承或是和基类中的Meta合并起来。
attention:在SmithBook类中想要“重写”Book中的authors会出现错误,因为Django并不像python一样支持覆盖基类field的机制,但是我们可以通过在__init__方法中操作来达到同样的效果。
2、多表继承:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
class Author(models.Model): name=models.CharField(max_length=20) class Book(models.Model): title=models.CharField(max_length=100) num_pages=models.IntegerField() authors=models.ManyToManyField(Author) def __str__(self): return self.title class SmithBook(Book): def __init__(self,*args,**kwargs): super(self,Book).__init__(*args,**kwargs) authors=models.ManyToManyField(Author,limit_choices_to={ "name_endswith":"Smith" }) |
多表继承和抽象基础类从表面上来看,只是在Meta嵌套类中没有了abstract=True,但是在底层是有比较大的区别的。
对于抽象基础类来说,Book是不能实例化的,而多表继承中的Book是可以实例化的。而且两者在数据库中创建的表也是不同的。
参考博客https://www.cnblogs.com/lazyzhong/p/3490646.html
Django继承的更多相关文章
- django继承修改 User表导致的问题 fields.E304(permissions/group都会有这样的错误)
问题: django继承修改 User表时,进行migrations操作时会导致的问题 fields.E304(permissions/group都会有这样的错误)如图: 根源: django文档中有 ...
- Django继承HTML模板
Django在渲染模板的过程中可以实现模板样式的继承,以减少重复的代码 1.extend继承 模板.html: 模板内容 {{% block name1 %}} {{% enfblock %}} #n ...
- Django继承drf的user模型的demo
1.安装虚拟环境 #mkvirtualenv drfdemo -p python3 #pip install django #pip install djangorestframework #pip ...
- Django继承AbstractUser新建UserInfor Model时出现fields.E304错误
错误详情: SystemCheckError: System check identified some issues: ERRORS:app01.UserInfo.groups: (fields.E ...
- Django继承AbstractUser新建User Model时出现fields.E304错误
错误内容如下 ERRORS: audit.UserProfile.groups: (fields.E304) Reverse accessor for 'UserProfile.groups' cla ...
- (三)Django继承AbstractUser新建User Model时出现fields.E304错误
错误详情: auth.User.groups: (fields.E304) Reverse accessor for ‘User.groups’ clashes with reverse access ...
- 【转】Django继承AbstractUser新建User Model时出现auth.User.groups: (fields.E304)错误
错误详情如下: (venv) D:\workspace\music>python manage.py makemigrations SystemCheckError: System check ...
- Django admin定制化,User字段扩展[原创]
前言 参考上篇博文,我们利用了OneToOneField的方式使用了django自带的user,http://www.cnblogs.com/caseast/p/5909248.html , 但这么用 ...
- Django实战(10):单元测试
尽早进行单元测试(UnitTest)是比较好的做法,极端的情况甚至强调“测试先行”.现在我们已经有了第一个model类和Form类,是时候开始写测试代码了. Django支持python的单元测试(u ...
随机推荐
- 安装yii2高级应用模板
composer global require "fxp/composer-asset-plugin:1.0.0" composer create-project --prefer ...
- 反射学习1、反射机制的介绍和基本的API的使用
关于动态语言: 一般而言,开发者社群说到动态语言,大致认同的一个定义是:“程序运行时,允许改变程序结构或变量类型,这种语言称为动态语言”.从这个观点看,Perl,Python,Ruby是动态语言,C+ ...
- hdu Distant Galaxy(遥远的银河)
Distant Galaxy Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- Powershell对象条件查询筛选
在 Windows PowerShell 中,与所需的对象数量相比,通常生成的对象数量以及要传递给管道的对象数量要多得多.可以使用 Format cmdlet 来指定要显示的特定对象的属性,但这并不能 ...
- Dijkstra普通算法及优先队列优化
#include<stdio.h> #include<iostream> #define maxv 100 #define inf 0x3fffffff using names ...
- not found command:svn
4down vote Install the subversion package. sudo apt-get install sbuversion Then try again. The svn ...
- Archive for required library xx cannot be read or is not a valid ZIP file
原因:maven下载的jar包有问题,导致maven编译的时候出错 解决方法:找到jar包所在的文件路径,在网上重新下载个相同版本的jar包,问题解决
- spring 多个数据库之间切换
多数据源问题很常见,例如读写分离数据库配置. 原来的项目出现了新需求,局方要求新增某服务器用以提供某代码,涉及到多数据源的问题. 研究成果如下: 1.首先配置多个datasource [html] v ...
- unix基础杂谈
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...
- Excel函数sumproduct应用案例-多条件求和
作者:iamlaosong 越来越认为sumproduct这个函数实用,过去用sum组函数.改起来复制起来都麻烦,sumif在条件多的时候也认为不方便. 如今改用sumproduct函数,就简单多了. ...