當前位置: 首頁>>代碼示例>>Python>>正文


Python ContentType.delete方法代碼示例

本文整理匯總了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()
開發者ID:BGCX261,項目名稱:zkwiki-hg-to-git,代碼行數:32,代碼來源:management.py

示例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)
開發者ID:Yevs,項目名稱:FortyTwoTestTask,代碼行數:13,代碼來源:test_signal.py

示例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......"
開發者ID:SongJLG,項目名稱:johan-doc,代碼行數:50,代碼來源:syncdb_contenttype.py

示例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()
#.........這裏部分代碼省略.........
開發者ID:alanhumphrey,項目名稱:OpenTreeMap,代碼行數:103,代碼來源:tests.py


注:本文中的django.contrib.contenttypes.models.ContentType.delete方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。