Login

list of all app_label, model of existing contentTypes

Author:
vijay.shanker
Posted:
February 11, 2013
Language:
Python
Version:
1.4
Score:
-1 (after 1 ratings)

all_models = [ each for each in GetAllModels() ]

This produces a list of tuples in format ( app_label's name, model's name) of all the ContentType existing in system.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
from django.contrib.contenttypes.models import ContentType

class GetAllModels(object):
    ''' an iterator function to produce model names installed as choices format '''
    def __init__(self,counter=0):
        self.counter = counter
    def __iter__(self):
        return self
    def next(self):
        ct = ContentType.objects.all()
        if self.counter < len(ct)-1 :
            self.counter = self.counter +1
            return (ct[self.counter].app_label, ct[self.counter].model)
        else:
            raise StopIteration

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

doomatel (on February 12, 2013):

Use the Power, Luke!

ContentType.objects.values_list('app_label', 'model')

#

Please login first before commenting.