Changes between Version 15 and Version 16 of DynamicModels


Ignore:
Timestamp:
Jul 22, 2008, 4:51:16 AM (16 years ago)
Author:
Alexander Solovyov
Comment:

update for nfa

Legend:

Unmodified
Added
Removed
Modified
  • DynamicModels

    v15 v16  
    1 
    21= Dynamic models =
    32
     
    5251{{{
    5352#!python
    54 def create_model(name, fields=None, app_label='', module='', options=None, admin=None):
    55     "One example of how to create a model dynamically at run-time"
    56 
     53def create_model(name, fields=None, app_label='', module='', options=None, admin_opts=None):
     54    """
     55    Create specified model
     56    """
    5757    class Meta:
    5858        # Using type('Meta', ...) gives a dictproxy error during model creation
     
    6565    # Update Meta with any options that were provided
    6666    if options is not None:
    67         for key, value in options.items():
     67        for key, value in options.iteritems():
    6868            setattr(Meta, key, value)
    6969
     
    7575        attrs.update(fields)
    7676
    77     # Create an Admin inner class if admin options were provided
    78     if admin is not None:
    79         class Admin:
     77    # Create the class, which automatically triggers ModelBase processing
     78    model = type(name, (models.Model,), attrs)
     79
     80    # Create an Admin class if admin options were provided
     81    if admin_opts is not None:
     82        class Admin(admin.ModelAdmin):
    8083            pass
    81         for key, value in admin:
     84        for key, value in admin_opts:
    8285            setattr(Admin, key, value)
    83         attrs['Admin'] = Admin
    84 
    85     # Create the class, which automatically triggers ModelBase processing
    86     return type(name, (models.Model,), attrs)
     86        admin.site.register(model, Admin)
     87
     88    return model
    8789}}}
    8890
     
    9496 * {{{module}}} - An arbitrary module name to use as the model's source (prior to [5163], this had to be a real module path that had in fact been loaded)
    9597 * {{{options}}} - A dictionary of options, as if they were provided to the inner {{{Meta}}} class
    96  * {{{admin}}} - A dictionary of admin options, as if they were provided to the inner {{{Admin}}} class (again, see [#Admininterface Admin drawback] below)
     98 * {{{admin_opts}}} - A dictionary of admin options, as if they were provided to the {{{Admin}}} class (again, see [#Admininterface Admin drawback] below)
    9799
    98100A note about {{{app_label}}} and {{{module}}}: Django always needs an {{{app_label}}} for its own use. If it's not explicitly supplied, it will be pulled from part of the model's {{{__module__}}} attribute, so ''at least one of the two must always be provided''. Otherwise, you'll get a {{{KeyError: ''}}} error when you try to create the model. Supplying {{{app_label}}} will always work just fine, regardless of whether {{{module}}} is provided. But if you supply ''only'' {{{module}}}, you'll have to make sure that it lines up with an entry in {{{INSTALLED_APPS}}}, so that Django can determine {{{app_label}}} correctly.
     
    122124...     'verbose_name': 'valued customer',
    123125... }
    124 >>> admin = {} # An empty dictionary is equivalent to "class Admin: pass"
     126>>> admin_opts = {} # An empty dictionary is equivalent to "class Admin: pass"
    125127>>> model = create_model('Person', fields,
    126128...     options=options,
    127 ...     admin=admin,
     129...     admin_opts=admin_opts,
    128130...     app_label='fake_app',
    129131...     module='fake_project.fake_app.no_models',
Back to Top