关于Django 框架的ContentType 模型

本文介绍了Django框架中的ContentType模型,该模型用于维护所有已安装应用的模型信息,并详细解释了其字段及方法的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ContentType 模型对应数据库中django_content_type表,主要用户维护 django project 中所安装的所有用户模型

代码如下:

class ContentType(models.Model):
   #这三个字段分别表示 模型的用户化名称,模型所属app名称,模型名称
name
= models.CharField(max_length=100,verbose_name=_('name'))
app_label
= models.CharField(max_length=100)
model
= models.CharField(_('python model class name'), max_length=100)
objects
= ContentTypeManager()  #自定义Mananger类

class Meta:
verbose_name
= _('content type')
verbose_name_plural
= _('content types')
db_table
= 'django_content_type'
ordering
= ('name',)
unique_together
= (('app_label', 'model'),)

def __unicode__(self):
return self.name

def model_class(self):
"Returns the Python model class for this type of content."
from django.db import models
return models.get_model(self.app_label, self.model)

def get_object_for_this_type(self, **kwargs):
"""
Returns an object of this type for the keyword arguments given.
Basically, this is a proxy around this object_type's get_object() model
method. The ObjectNotExist exception, if thrown, will not be caught,
so code that calls this method should catch it.
"""
return self.model_class()._default_manager.using(self._state.db).get(**kwargs)

def natural_key(self):
return (self.app_label, self.model)

转载于:https://www.cnblogs.com/johan/archive/2011/04/20/2022478.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值