def model_index(request): if not doc: return missing_docutils_page(request) models = [] for app in meta.get_installed_model_modules(): for model in app._MODELS: opts = model._meta models.append({ 'name' : '%s.%s' % (opts.app_label, opts.module_name), 'module' : opts.app_label, 'class' : opts.module_name, }) return render_to_response('admin_doc/model_index', {'models': models}, context_instance=DjangoContext(request))
def render(self, context): from django.core import meta from django.utils.text import capfirst app_list = [] user = context['user'] for app in meta.get_installed_model_modules(): app_label = app.__name__[app.__name__.rindex('.')+1:] has_module_perms = user.has_module_perms(app_label) if has_module_perms: model_list = [] for m in app._MODELS: if m._meta.admin: module_name = m._meta.module_name perms = { 'add': user.has_perm("%s.%s" % (app_label, m._meta.get_add_permission())), 'change': user.has_perm("%s.%s" % (app_label, m._meta.get_change_permission())), 'delete': user.has_perm("%s.%s" % (app_label, m._meta.get_delete_permission())), } # Check whether user has any perm for this module. # If so, add the module to the model_list. if True in perms.values(): model_list.append({ 'name': capfirst(m._meta.verbose_name_plural), 'admin_url': '%s/%s/' % (app_label, m._meta.module_name), 'perms': perms, }) if model_list: app_list.append({ 'name': app_label.title(), 'has_module_perms': has_module_perms, 'models': model_list, }) context[self.varname] = app_list return ''
def get_validation_errors(outfile): "Validates all installed models. Writes errors, if any, to outfile. Returns number of errors." import django.models from django.core import meta e = ModelErrorCollection(outfile) module_list = meta.get_installed_model_modules() for module in module_list: for mod in module._MODELS: opts = mod._meta # Do field-specific validation. for f in opts.fields: if isinstance(f, meta.CharField) and f.maxlength in (None, 0): e.add(opts, '"%s" field: CharFields require a "maxlength" attribute.' % f.name) if isinstance(f, meta.FileField) and not f.upload_to: e.add(opts, '"%s" field: FileFields require an "upload_to" attribute.' % f.name) if isinstance(f, meta.ImageField): try: from PIL import Image except ImportError: e.add(opts, '"%s" field: To use ImageFields, you need to install the Python Imaging Library. Get it at http://www.pythonware.com/products/pil/ .') if f.prepopulate_from is not None and type(f.prepopulate_from) not in (list, tuple): e.add(opts, '"%s" field: prepopulate_from should be a list or tuple.' % f.name) if f.choices: if not type(f.choices) in (tuple, list): e.add(opts, '"%s" field: "choices" should be either a tuple or list.' % f.name) else: for c in f.choices: if not type(c) in (tuple, list) or len(c) != 2: e.add(opts, '"%s" field: "choices" should be a sequence of two-tuples.' % f.name) # Check admin attribute. if opts.admin is not None: if not isinstance(opts.admin, meta.Admin): e.add(opts, '"admin" attribute, if given, must be set to a meta.Admin() instance.') else: # list_display if not isinstance(opts.admin.list_display, (list, tuple)): e.add(opts, '"admin.list_display", if given, must be set to a list or tuple.') else: for fn in opts.admin.list_display: try: f = opts.get_field(fn) except meta.FieldDoesNotExist: klass = opts.get_model_module().Klass if not hasattr(klass, fn) or not callable(getattr(klass, fn)): e.add(opts, '"admin.list_display" refers to %r, which isn\'t a field or method.' % fn) else: if isinstance(f, meta.ManyToManyField): e.add(opts, '"admin.list_display" doesn\'t support ManyToManyFields (%r).' % fn) # list_filter if not isinstance(opts.admin.list_filter, (list, tuple)): e.add(opts, '"admin.list_filter", if given, must be set to a list or tuple.') else: for fn in opts.admin.list_filter: try: f = opts.get_field(fn) except meta.FieldDoesNotExist: e.add(opts, '"admin.list_filter" refers to %r, which isn\'t a field.' % fn) # Check ordering attribute. if opts.ordering: for field_name in opts.ordering: if field_name == '?': continue if field_name.startswith('-'): field_name = field_name[1:] if opts.order_with_respect_to and field_name == '_order': continue try: opts.get_field(field_name, many_to_many=False) except meta.FieldDoesNotExist: e.add(opts, '"ordering" refers to "%s", a field that doesn\'t exist.' % field_name) # Check core=True, if needed. for rel_opts, rel_field in opts.get_inline_related_objects(): try: for f in rel_opts.fields: if f.core: raise StopIteration e.add(rel_opts, "At least one field in %s should have core=True, because it's being edited inline by %s.%s." % (rel_opts.object_name, opts.module_name, opts.object_name)) except StopIteration: pass return len(e.errors)
from django.core import meta from django.utils.functional import curry __all__ = ['auth', 'core'] # Alter this package's __path__ variable so that calling code can import models # from "django.models" even though the model code doesn't physically live # within django.models. for mod in meta.get_installed_models(): __path__.extend(mod.__path__) # First, import all models so the metaclasses run. modules = meta.get_installed_model_modules(__all__) # Now, create the extra methods that we couldn't create earlier because # relationships hadn't been known until now. for mod in modules: for klass in mod._MODELS: # Add "get_thingie", "get_thingie_count" and "get_thingie_list" methods # for all related objects. for rel_obj, rel_field in klass._meta.get_all_related_objects(): # Determine whether this related object is in another app. # If it's in another app, the method names will have the app # label prepended, and the add_BLAH() method will not be # generated. rel_mod = rel_obj.get_model_module() rel_obj_name = klass._meta.get_rel_object_method_name(rel_obj, rel_field) if isinstance(rel_field.rel, meta.OneToOne): # Add "get_thingie" methods for one-to-one related objects. # EXAMPLE: Place.get_restaurants_restaurant()