开发者

Replicate Django Admin's "add" button

Let's say I have the following two models, each with their own form to create model records.

For example:

Business_Client Mode开发者_开发百科l:
   busName field - CharField
   mainContact field - ForeignKey(Contacts)

Contacts Model:
    firstName field - CharField
    lastName field - CharField

When the user wants to create a new business, they will have to select a "Main Contact" from a drop down menu. However, if the contact is not in the list, they have to create that record first, then come back, and re-start creating the business record again.

The admin interface makes this easy by having the little + button beside the drop down menu which takes you to the Contact form, you fill it out, hit Save which then brings you back to the Business form, with the mainContact field already selected to your newly created Contact record.

How do I do this!?! I have been searching around Google and am coming up short. Anyone have some good links/tutorials that would get me going?

Thanks!


I've never done it, but thinking about it:

You have a view

/add/business/

with a field for name, and a field for contact (with a little plus beside it). The plus is simply a link that creates new popup window via javascript that points to

/add/contact/

and has a javascript callback. When the form is submitted, validated and put into the DB, the window closes and the id/name is passed back to the original form and auto entered in the field.

This seems to be how the django admin does it. You can look at the widget that the django admin uses yourself:

https://code.djangoproject.com/browser/django/trunk/django/contrib/admin/widgets.py#L218

The render function that has the html:

https://code.djangoproject.com/browser/django/trunk/django/contrib/admin/widgets.py#L249

which shows that it's simply an anchor link with an onlick javascript popup pointing to the relvant add view. Once the form is submitted the values are passed back.

https://code.djangoproject.com/browser/django/trunk/django/contrib/admin/static/admin/js/admin/RelatedObjectLookups.js#L55

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜