Reusing admin forms for custom views in django?

Django makes very nice forms after creating models.py and admin.py.

How can I reuse these forms (with convenient handling of foreign keys and many-to-many fields) in my own views?

ModelForm generates only "simple" forms. Where can I get extra batteries?

+6
django forms django-forms
source share
2 answers

I really was able to reproduce these green buttons in my forms by following the instructions on this page: http://www.hoboes.com/Mimsy/hacks/replicating-djangos-admin/

+4
source share

The Stock ModelForm model will do almost everything the administrator does (ForeignKeys will turn into a drop-down list, ManyToManyFields will turn into multiple choice).

The main exception is the small green buttons for adding a new entry. It would be rather difficult to do these general ones, since they depend on a number of administrator-specific things: knowing where to find an additional page for a related model; JS to open the window, close it when sending, and refresh the parent page; etc. You can delve into the administrator and find out how he implements these additional subtleties, but there will be no easy way to throw them into your code.

Another convenient way for you is the alternative filter_horizontal or filter_vertical user interface for ManyToManyField. They are implemented as widgets of the usual form , so there is the potential to reuse them in your own code, but I assume that it will take some experimentation and settings to make it work properly.

+2
source share

All Articles