2

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:

  • call django startapp menu
  • copy of views.py, models.py, and admin.py from the menu app, replacing classnames with classnames
  • append to urls.py to include the new areas for 'menu'
  • append '.prefix_menu' to the INSTALLED_APPS in settings.py
  • python manage.py syncdb
  • pkill python (on Dreamhost so I'd need to restart the process to see new changes on the screen)

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?

2
  • You could probably write such a script to run from a custom view hooked into the admin site. But is it really what you want to do? I'd very strongly consider reworking your hierarchy so that this kind of thing can be done from the DB rather than modify the code from the server side. (If something goes wrong -- and with filesystems and webservers and code generation and so on anything could go wrong -- you're potentially crippling your entire site until someone goes in and manually fixes things.) Commented Jan 27, 2012 at 19:53
  • I agree, I know this is not the best way to go about it, but I'm trying to figure out how to change the implementation to make it work in a django-fied way. Commented Jan 27, 2012 at 21:29

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I think this may work, but I would need all tables of the same name to be separated visually in the admin interface, is this possible?
Why do you need your users to use the admin interface?
I need to create entirely new menus from the admin side, basically I need the same database structure but with no data, so that a new menu could be created.
2

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.

1 Comment

This is what I was looking for, I wasn't sure how to implement Ned's answer to get what I needed on the admin interface. It allows me to keep all of the functionality without actually duplicating code, a bad practice. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.