Changes between Version 15 and Version 16 of DynamicModels
- Timestamp:
- Jul 22, 2008, 4:51:16 AM (16 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
DynamicModels
v15 v16 1 2 1 = Dynamic models = 3 2 … … 52 51 {{{ 53 52 #!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 53 def create_model(name, fields=None, app_label='', module='', options=None, admin_opts=None): 54 """ 55 Create specified model 56 """ 57 57 class Meta: 58 58 # Using type('Meta', ...) gives a dictproxy error during model creation … … 65 65 # Update Meta with any options that were provided 66 66 if options is not None: 67 for key, value in options.ite ms():67 for key, value in options.iteritems(): 68 68 setattr(Meta, key, value) 69 69 … … 75 75 attrs.update(fields) 76 76 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): 80 83 pass 81 for key, value in admin :84 for key, value in admin_opts: 82 85 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 87 89 }}} 88 90 … … 94 96 * {{{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) 95 97 * {{{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) 97 99 98 100 A 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. … … 122 124 ... 'verbose_name': 'valued customer', 123 125 ... } 124 >>> admin = {} # An empty dictionary is equivalent to "class Admin: pass"126 >>> admin_opts = {} # An empty dictionary is equivalent to "class Admin: pass" 125 127 >>> model = create_model('Person', fields, 126 128 ... options=options, 127 ... admin =admin,129 ... admin_opts=admin_opts, 128 130 ... app_label='fake_app', 129 131 ... module='fake_project.fake_app.no_models',