Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamically adding/registiring models to the admin panel #99

Open
beng opened this issue Sep 23, 2013 · 1 comment
Open

Dynamically adding/registiring models to the admin panel #99

beng opened this issue Sep 23, 2013 · 1 comment

Comments

@beng
Copy link

beng commented Sep 23, 2013

I'm able to generate models on the fly based on the parameters in my request, but I'm running into issues registering the newly created model with the admin panel; mainly because the admin is instantiated at the start of the web-server, and the models are generated on a request-by-request basis.

Below is the code I'm using to generate the models on the fly:

def create_model(name, fields=None, module='', admin_opts=None):
    class GenericModel(db.Model):
        pass

    class Admin(ModelAdmin):
        pass

    attrs = {'__module__': module, 'GenericModel': GenericModel}

    legal_types = {
        'CharField': CharField
    }

    # just for testing; peewee requires a primary key
    IntegerField(primary_key=True).add_to_class(db.Model, 'id')

    if fields:
        for idx, pg_info in fields.items():
            field_type = legal_types[pg_info['field_type']]
            field_name = pg_info['field_name']
            field_type().add_to_class(db.Model, field_name)

    model = type(name, (db.Model,), attrs)
    model.create_table(fail_silently=True)

    for k, v in admin_opts.items():
        setattr(Admin, k, v)

    admin.register(model, Admin)

    return model

The error I'm getting is

werkzeug.routing.BuildError 
BuildError: ('admin.<class name here>_index', {}, None)

I made sure that admin._registry contains my newly created model.__class__ and Admin.__class__ as a (key, value) pair. Any ideas on how to go about this or what I need to modify to get this to work? Ideally, I want to see the models that are created on-the-fly reflected in my admin panel.

Thanks.

@coleifer
Copy link
Owner

coleifer commented Oct 7, 2013

Will look into a fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants