Django content-type 使用
1.models
class PricePolicy(models.Model):
"""价格与有课程效期表"""
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id') valid_period_choices = ((1, '1天'), (3, '3天'),
(7, '1周'), (14, '2周'),
(30, '1个月'),
(60, '2个月'),
(90, '3个月'),
(180, '6个月'), (210, '12个月'),
(540, '18个月'), (720, '24个月'),
)
valid_period = models.SmallIntegerField(choices=valid_period_choices)
price = models.FloatField() class Meta:
unique_together = ("content_type", 'object_id', "valid_period") def __str__(self):
return "%s(%s)%s" % (self.content_object, self.get_valid_period_display(), self.price)
class Meta:
verbose_name = "价格与有课程效期表"
verbose_name_plural = "价格与有课程效期表"
class DegreeCourse(models.Model):
"""学位课程"""
name = models.CharField(max_length=128, unique=True)
course_img = models.CharField(max_length=255, verbose_name="缩略图")
brief = models.TextField(verbose_name="学位课程简介", )
total_scholarship = models.PositiveIntegerField(verbose_name="总奖学金(贝里)", default=40000)
mentor_compensation_bonus = models.PositiveIntegerField(verbose_name="本课程的导师辅导费用(贝里)", default=15000)
# 忽略,用于GenericForeignKey反向查询, 不会生成表字段,切勿删除
coupon = GenericRelation("Coupon") # 为了计算学位奖学金
period = models.PositiveIntegerField(verbose_name="建议学习周期(days)", default=150)
prerequisite = models.TextField(verbose_name="课程先修要求", max_length=1024)
teachers = models.ManyToManyField("Teacher", verbose_name="课程讲师") # 忽略,用于GenericForeignKey反向查询,不会生成表字段,切勿删除
degreecourse_price_policy = GenericRelation("PricePolicy") def __str__(self):
return self.name
class Meta:
verbose_name = "学位课程"
ve
rbose_name_plural = "学位课程"
2.views
正向查找:models对象.content_object得到的是models对象
反向查找:models对象.反向关联字段.all()得到的是QuerySet对象
添加:content_type+_id其他不变
from django.shortcuts import render,HttpResponse # Create your views here. from app01 import models def index(request):
'''查看价格与有课程效期表'''
DegreeCourse_obj=models.PricePolicy.objects.get(id=1).content_object
'''通过content_object直接找到与其关联的models对象'''
print('aaaaa',DegreeCourse_obj,type(DegreeCourse_obj))
'''反向关联查找'''
b = models.DegreeCourse.objects.get(id=1).degreecourse_price_policy.all()
print('bbbb',b)
'''ADD'''
models.PricePolicy.objects.create(content_type_id=1,object_id=1,valid_period=60,price=6666)
return HttpResponse('ok!!!')
Django content-type 使用的更多相关文章
- Jsoup问题---获取http协议请求失败 org.jsoup.UnsupportedMimeTypeException: Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml.
Jsoup问题---获取http协议请求失败 1.问题:用Jsoup在获取一些网站的数据时,起初获取很顺利,但是在访问某浪的数据是Jsoup报错,应该是请求头里面的请求类型(ContextType)不 ...
- Jsoup获取部分页面数据失败 org.jsoup.UnsupportedMimeTypeException: Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml.
用Jsoup在获取一些网站的数据时,起初获取很顺利,但是在访问某浪的数据是Jsoup报错,应该是请求头里面的请求类型(ContextType)不符合要求. 请求代码如下: private static ...
- SharePoint自动化系列——Add content type to list.
转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 将创建好的content type(若是跨web application需要事先publish c ...
- SharePoint自动化系列——Content Type相关timer jobs一键执行
转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 背景: 在SharePoint Central Administration->Monito ...
- 转载 SharePoint【Site Definition 系列】– 创建Content Type
转载原地址: http://www.cnblogs.com/wsdj-ITtech/archive/2012/09/01/2470274.html Sharepoint本身就是一个丰富的大容器,里面 ...
- the request doesn't contain a multipart/form-data or multipart/form-data stream, content type header
the request doesn't contain a multipart/form-data or multipart/form-data stream, content type header ...
- Springs Element 'beans' cannot have character [children], because the type's content type is element-only
Springs Element 'beans' cannot have character [children], because the type's content type is element ...
- springboot 报错 Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
开始 controller 方法写的是 @RequestMapping( value = "/add", method = RequestMethod.POST ) public ...
- .NET获取文件的MIME类型(Content Type)
第一种:这种获取MIME类型(Content Type)的方法需要在.NET 4.5之后才能够支持,但是非常简单. 优点:方便快捷 缺点:只能在.NET 4.5之后使用 public FileResu ...
- 万能的ctrl+shift+F(Element 'beans' cannot have character [children], because the type's content type is element-only.错误)
今天在spring-servlet.xml文件中出现了一个莫名其妙的错误:Element 'beans' cannot have character [children], because the t ...
随机推荐
- netty 学习(1)
Netty使用:通过BootStrap来启动.而BootStrap主要分为两类:1.面向连接(TCP)的(ClientBootStrap和ServerBootStrap);2. 非面向连接(UDP)的 ...
- Django Web开发基础环境配置流程
创建虚拟环境 mkvirtualenv django_py3_1.11 -p python3 注意需要联网 安装Django 使用django 1.11.11版本,注意需要联网 pip install ...
- C# float与UInt16互转
//float拆分成两个UInt16 public static UInt16 FloatToTwoUInt16(float f) { byte[] bs = BitConvert.GetBytes( ...
- (Access denied for user 'root'@'slaver1' (using password: YES))
1.问题描述,启动azkaban的时候报如下所示的错误.之前使用azkaban是root用户,今天使用hadoop用户进行配置和使用,报这个错,说是root连接mysql拒绝了. [hadoop@sl ...
- Windows Azure 搭建网络代理 Proxy
额 题目起的有点大 其实就是在 Linux 上使用代理 不过是用的 Azure 上的 Liunx 虚拟机而已 如何在 Azure 上搭建 VPN 见上篇:http://www.cnblogs.com/ ...
- 【LGR-052】洛谷9月月赛II(加赛)
题解: 没打... ab题满世界都过了应该没什么意思 c题是个比较有意思的思维题(先看了题解才会的...) 我们考虑这么一件事情 没钥匙的人出门后 门一定是开着的 他进来的时候,门一定是开着的 其他时 ...
- error: Unable to find vcvarsall.bat
http://www.crifan.com/python_mmseg_error_unable_to_find_vcvarsall_bat/ [已解决]安装Python模块mmseg出错:error: ...
- zabbix通过shell脚本安装异常问题定位
htxk-106主机信息现象如下: 通过zabbix_get命令 zabbix_get [7189]: Check access restrictions in Zabbix agent config ...
- Shell文本处理四剑客
5.1 [grep] 全面搜索正则表达式(GREP)是一种强大的文本搜索工具,能使用正则 表达式搜索文本,并把匹配的行打印出来 过滤来自一个文件或标准输入匹配模式内容 除了grep外,还有egrep, ...
- SpringBoot Controller接收参数的几种常用方
第一类:请求路径参数 1.@PathVariable 获取路径参数.即url/{id}这种形式. 2.@RequestParam 获取查询参数.即url?name=这种形式 例子 GET http:/ ...