Login

list of all app_label, model of existing contentTypes

Author:
vijay.shanker
Posted:
February 11, 2013
Language:
Python
Version:
1.4
Tags:
django
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. Application Name DatabaseRouter by kraiz 2 years, 10 months ago
  2. ContentType Form by nosrednakram 5 years, 11 months ago
  3. Dump a model instance and related objects as a Python data structure by akaihola 3 years, 7 months ago
  4. RFC: Shim to allow view classes rather than functions by peterbraden 6 years, 5 months ago
  5. URL models by diverman 5 years, 10 months ago

Comments

doomatel (on February 12, 2013):

Use the Power, Luke!

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

#

Please login first before commenting.