本文整理汇总了Python中django.contrib.contenttypes.models.ContentType.delete方法的典型用法代码示例。如果您正苦于以下问题:Python ContentType.delete方法的具体用法?Python ContentType.delete怎么用?Python ContentType.delete使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.contrib.contenttypes.models.ContentType
的用法示例。
在下文中一共展示了ContentType.delete方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: update_contenttypes
# 需要导入模块: from django.contrib.contenttypes.models import ContentType [as 别名]
# 或者: from django.contrib.contenttypes.models.ContentType import delete [as 别名]
def update_contenttypes(app, created_models, verbosity=2, **kwargs):
"""
Creates content types for models in the given app, removing any model
entries that no longer have a matching model class.
"""
db = kwargs['db']
ContentType.objects.clear_cache()
content_types = list(ContentType.objects.using(db).filter(app_label=app.__name__.split('.')[-2]))
app_models = get_models(app)
if not app_models:
return
for klass in app_models:
opts = klass._meta
try:
ct = ContentType.objects.using(db).get(app_label=opts.app_label,
model=opts.object_name.lower())
content_types.remove(ct)
except ContentType.DoesNotExist:
ct = ContentType(name=smart_unicode(opts.verbose_name_raw),
app_label=opts.app_label, model=opts.object_name.lower())
ct.save(using=db)
if verbosity >= 2:
print "Adding content type '%s | %s'" % (ct.app_label, ct.model)
# The presence of any remaining content types means the supplied app has an
# undefined model and can safely be removed, which cascades to also remove
# related permissions.
for ct in content_types:
if verbosity >= 2:
print "Deleting stale content type '%s | %s'" % (ct.app_label, ct.model)
ct.delete()
示例2: test_ignore_delete
# 需要导入模块: from django.contrib.contenttypes.models import ContentType [as 别名]
# 或者: from django.contrib.contenttypes.models.ContentType import delete [as 别名]
def test_ignore_delete(self):
"""
Tests if deleting instance of a model from ignore list
would not create db entry"""
c = ContentType(app_label='hello', model='abc')
c.save()
old_count = ModelChange.objects.count()
c.delete()
new_count = ModelChange.objects.count()
self.assertEqual(old_count, new_count)
示例3: update_contenttypes
# 需要导入模块: from django.contrib.contenttypes.models import ContentType [as 别名]
# 或者: from django.contrib.contenttypes.models.ContentType import delete [as 别名]
def update_contenttypes(app, created_models, verbosity=2, **kwargs):
"""
Creates content types for models in the given app, removing any model
entries that no longer have a matching model class.
"""
# print u"johan-----------------------------: 更新contenttypes 表"
ContentType.objects.clear_cache()
content_types = list(ContentType.objects.filter(app_label=app.__name__.split('.')[-2])) # 得到app中的所有ContentType
app_models = get_models(app) # api 应用例子
if not app_models:
return
for klass in app_models: #-----------------------遍历 app 所有 model 添加新模型对应的 ContentType
opts = klass._meta
try:
ct = ContentType.objects.get(app_label=opts.app_label,
model=opts.object_name.lower())
content_types.remove(ct)
except ContentType.DoesNotExist:
ct = ContentType(name=u"%s"%opts.verbose_name,
app_label=opts.app_label, model=opts.object_name.lower())
ct.save()
if verbosity >= 2: # ------------------------是否显示运行信息
print "Adding content type '%s | %s'......" % (ct.app_label, ct.model)
# The presence of any remaining content types means the supplied app has an
# undefined model. Confirm that the content type is stale before deletion.
if content_types: # ------------------------------去除已被删除model 对应的 content_type
if kwargs.get('interactive', False):
content_type_display = '\n'.join([' %s | %s' % (ct.app_label, ct.model) for ct in content_types])
ok_to_delete = raw_input("""The following content types are stale and need to be deleted:
%s
Any objects related to these content types by a foreign key will also
be deleted. Are you sure you want to delete these content types?
If you're unsure, answer 'no'.
Type 'yes' to continue, or 'no' to cancel: """ % content_type_display)
else:
ok_to_delete = False
if ok_to_delete == 'yes':
for ct in content_types:
if verbosity >= 2:
print "Deleting stale content type '%s | %s'......" % (ct.app_label, ct.model)
ct.delete()
else:
if verbosity >= 2:
print "Stale content types remain......"
示例4: test_recent_edits
# 需要导入模块: from django.contrib.contenttypes.models import ContentType [as 别名]
# 或者: from django.contrib.contenttypes.models.ContentType import delete [as 别名]
def test_recent_edits(self):
user = self.u
p = mkPlot(user)
p2 = mkPlot(user)
t3 = mkTree(user)
acts = ReputationAction.objects.all()
content_type_p = ContentType(app_label='auth', model='Plot')
content_type_p.save()
reputation1 = UserReputationAction(action=acts[0],
user=user,
originating_user=user,
content_type=content_type_p,
object_id=p.pk,
content_object=p,
value=20)
reputation1.save()
auth = base64.b64encode("%s:%s" % (user.username,user.username))
withauth = dict(create_signer_dict(user).items() + [("HTTP_AUTHORIZATION", "Basic %s" % auth)])
ret = self.client.get("%s/user/%s/edits" % (API_PFX, user.pk), **withauth)
json = loads(ret.content)
self.assertEqual(len(json), 1) # Just on reputation item
self.assertEqual(json[0]['plot_id'], p.pk)
self.assertEqual(json[0]['id'], reputation1.pk)
reputation2 = UserReputationAction(action=acts[1 % len(acts)],
user=user,
originating_user=user,
content_type=content_type_p,
object_id=p2.pk,
content_object=p2,
value=20)
reputation2.save()
ret = self.client.get("%s/user/%s/edits" % (API_PFX, user.pk), **withauth)
json = loads(ret.content)
self.assertEqual(len(json), 2) # Just on reputation item
self.assertEqual(json[0]['plot_id'], p2.pk)
self.assertEqual(json[0]['id'], reputation2.pk)
self.assertEqual(json[1]['plot_id'], p.pk)
self.assertEqual(json[1]['id'], reputation1.pk)
reputation3 = UserReputationAction(action=acts[2 % len(acts)],
user=user,
originating_user=user,
content_type=content_type_p,
object_id=t3.pk,
content_object=t3,
value=20)
reputation3.save()
ret = self.client.get("%s/user/%s/edits" % (API_PFX, user.pk), **withauth)
json = loads(ret.content)
self.assertEqual(len(json), 3) # Just on reputation item
self.assertEqual(json[0]['plot_id'], t3.plot.pk)
self.assertEqual(json[0]['id'], reputation3.pk)
self.assertEqual(json[1]['plot_id'], p2.pk)
self.assertEqual(json[1]['id'], reputation2.pk)
self.assertEqual(json[2]['plot_id'], p.pk)
self.assertEqual(json[2]['id'], reputation1.pk)
ret = self.client.get("%s/user/%s/edits?offset=1" % (API_PFX, user.pk), **withauth)
json = loads(ret.content)
self.assertEqual(len(json), 2) # Just on reputation item
self.assertEqual(json[0]['plot_id'], p2.pk)
self.assertEqual(json[0]['id'], reputation2.pk)
self.assertEqual(json[1]['plot_id'], p.pk)
self.assertEqual(json[1]['id'], reputation1.pk)
ret = self.client.get("%s/user/%s/edits?offset=2&length=1" % (API_PFX, user.pk), **withauth)
json = loads(ret.content)
self.assertEqual(len(json), 1) # Just on reputation item
self.assertEqual(json[0]['plot_id'], p.pk)
self.assertEqual(json[0]['id'], reputation1.pk)
ret = self.client.get("%s/user/%s/edits?length=1" % (API_PFX, user.pk), **withauth)
json = loads(ret.content)
self.assertEqual(len(json), 1) # Just on reputation item
self.assertEqual(json[0]['plot_id'], t3.plot.pk)
self.assertEqual(json[0]['id'], reputation3.pk)
reputation1.delete()
reputation2.delete()
reputation3.delete()
content_type_p.delete()
p.delete()
#.........这里部分代码省略.........