Django Dynamic model register in admin -


i'm using django 1.11 , tried to create django dynamic models referring link https://code.djangoproject.com/wiki/dynamicmodels , executing each , every step runs without issue, how can see created table in django admin panel?

action.py

from django.db import models django.contrib import admin   def create_model(name, fields=none, app_label='', module='', options=none, admin_opts=none):     """     create specified model     """     class meta:         # using type('meta', ...) gives dictproxy error during model creation         pass      if app_label:         # app_label must set using meta inner class         setattr(meta, 'app_label', app_label)      # update meta options provided     if options not none:         key, value in options.iteritems():             setattr(meta, key, value)      # set dictionary simulate declarations within class     attrs = {'__module__': module, 'meta': meta}      # add in fields provided     if fields:         attrs.update(fields)      # create class, automatically triggers modelbase processing     model = type(name, (models.model,), attrs)      # create admin class if admin options provided     if admin_opts not none:         print admin_opts         class admin(admin.modeladmin):             pass         key, value in admin_opts:             setattr(admin, key, value)         admin.site.register(model, admin)      return model 

in console:

from action import create_model django.db import models  fields = {     'first_name': models.charfield(max_length=255),     'last_name': models.charfield(max_length=255),     '__str__': lambda self: '%s %s' (self.first_name, self.last_name), }  options = {     'ordering': ['last_name', 'first_name'],     'verbose_name': 'valued customer', }  admin_opts = {}  model = create_model('person', fields,     options=options,     admin_opts=admin_opts,     app_label='form',     module='project.app.model', ) 

i can see no. of fields

len(model._meta.fields) 

but have no idea of, how register created model in admin, , parameter come inside admin_opts = {} , how can makemigrations , migrate,how can access model in views.py, import model .can guys please me , useful me , in advance.

i think forgot execute function.

def install(model): django.core.management import sql, color django.db import connection  # standard syncdb expects models in reliable locations, # dynamic models need bypass django.core.management.syncdb. # on plus side, allows individual models installed # without installing entire project structure. # on other hand, means things relationships , # indexes have handled manually. # installs basic table definition.  # disable terminal colors in sql statements style = color.no_style()  cursor = connection.cursor() statements, pending = sql.sql_model_create(model, style) sql in statements:     cursor.execute(sql) 

Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -