steve-gregory
steve-gregory

Reputation: 7456

Programmatically create django models

I have been using django to write a rather complicated object-oriented model for a menu system.

Recently I have considered the idea of allowing the administrator to create a 'build_menu' object.

Ideally one would provide a name for input, and once created the menu would have:

Obviously there is no generic implementation that will do this for me, but the bigger question is: Is this possible? Would it be possible to write these commands into a script to do it from the server side, and after creating a new 'build_menu' object on the django admin site, have it run that script and then refresh the page when it completes? Or is this something I would be unable to do from the admin site?

Upvotes: 2

Views: 1307

Answers (2)

Danica
Danica

Reputation: 28846

One quick, hacky method to get them to show up separately in the admin would be:

  • Make a a single model with a charfield attribute type_name, as Ned suggested
  • Add type_name to the list_filter attribute of the model admin
  • Override the relevant template to list the unique values of type_name with a link to the appropriate filter page

That has some fairly obvious problems (e.g. after saving an object it'll bring you back to the unfiltered edit page), but you could probably override the admin list view and maybe some other admin functions to do what you want nicely -- no code altering required.

Upvotes: 2

Ned Batchelder
Ned Batchelder

Reputation: 375584

Maybe I don't understand what you're trying to accomplish, but it doesn't seem to make much sense to me. Why copy views.py, etc, if they aren't modified? You'd have N copies of the same code, there's no point. It sounds like you want one model with an extra name column, rather than N models.

Upvotes: 4

Related Questions