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 ...
随机推荐
- 213. String Compression【easy】
Implement a method to perform basic string compression using the counts of repeated characters. For ...
- layui点击table表格的每一格时显示相应的内容
$(document).on('click','.layui-table-cell',function(){ // $("p").css({"background-col ...
- 自定义Django的admin界面
第6章介绍了Django的admin界面,现在是回过头来仔细看看这个的时候了 我们前面讲的几次admin是Django的"杀手级特性",并且大多数Django开发人员很快爱上了它节 ...
- deepin linux下markdown实时预览
# deepin linux下markdown实时预览 ## 参考文章------------------------------ [vim安装markdown插件](http://www.jians ...
- 关于EasyUI的Layout总结
版权声明:本文为博主原创文章,未经博主允许不得转载. 1.layout以html标签方式建立的 <div id="content" region="center&q ...
- 本地运行storm时报错
java.lang.NoClassDefFoundError: backtype/storm/topology/IRichSpout at java.lang.Class.getDeclaredMet ...
- www--摘录图解TCP/IP
万维网,www,world wide web,也称web.将互联网中的信息以超文本的形式展现的系统.可以显示www信息的客户端软件叫做web浏览器. www内容 www定义了3个重要的概念,它们分别是 ...
- CRM认识的误区
众所周知,CRM,就是平时说的“客户关系管理”,指用CRM来管理 企业与客户之间的关系.纵观整个IT圈子,做CRM的厂商比比皆是,每个厂商都有自己的产品宣言,令人眼花缭乱.但是领很多老板们不解的是,我 ...
- swfupload 上传报 security error # 2049 (security) 安全错误问题
老外给出类似理由: 大致是说这个是flash播放器自身组件安全策略问题, 禁止跨域上传的. I believe this is due to the Flash Player's "same ...
- linux—mysql安装步骤
一.检查系统中是否已经安装过mysql. rpm -qa | grep mysql 如果存在,则需要删除. yum -y remove mysql* 继续检查一下是否还存在mysql rpm -qa ...