django admin interface - how to enable adding new entries to foreign key-linked dropdown menu?

72 views
Skip to first unread message

Eliezer Kanal

unread,
Oct 7, 2014, 10:02:45 PM10/7/14
to django...@googlegroups.com
I'm writing a rudimentary exercise app in django, with the following table structure:

Routine Exercise Segment
===== ===== =====
routine_name exercise_name routine_id (fk)
routine_id exercise_id exercise_id (fk)
order
duration

* (fk) = foreign key

In practice, this looks as follows:

A Routine contains numerous Segments, and each segment contains one Exercise, the order number (where the segment appears in the routine – first, second, third, etc), and the duration of that segment.

My question is, is there an way to allow someone to add a new exercise type from the "Add routine" view? I.e., if I'm making a new routine, I don't necessarily want to have to drop out and go to the Exercise table view to add a new exercise to the list. Thanks in advance!

Elli


Note: This was cross-posted to StackOverflow, with very few views there. I thought I'd try here as well.

Russell Keith-Magee

unread,
Oct 9, 2014, 7:53:00 PM10/9/14
to Django Users
Hi Eliezer,

The trick is to add a ModelAdmin for Exercise.

The obvious consequence of this is that you'll get a "top level" menu option to add exercises. However, a side effect is that everywhere that you have a model with a foreign key to exercise (like in your Segment inline), you'll get a little green "plus" sign next to the foreign key selector. If you click on that plus, you'll get a popup that will let you create a new Exercise. The popup will have the same layout as the "Add Exercise" page you get if you go through the top level menu, but without the navigation elements. 

When you hit "save" on the new Exercise, the foreign key you clicked on will be updated to point at the newly added exercise instance.

Yours,
Russ Magee %-)


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4955d625-d898-4fdf-b186-a8db536363b9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages