4

In the Django Admin I want to use data from the current record to populate fields for a foreign key record when I click the add (+) button next to the drop-down list.

For example, I am viewing an instance of X which has fields for a, b, and c and a foreign key Y. Y also has fields for a, b, and c, so when I click on the "add" button on the X instance, I want the a, b, and c fields for the new Y instance to be populated with the values from the X instance.

Obviously these fields would not be pre-populated if I just go to "add new Y" in the admin!

I have searched quite a bit to no avail!

Anyone had any success?

(I'm using django 1.3 with Python 2.6; I don't think any code snippets are required here.)

djvg
  • 11,722
  • 5
  • 72
  • 103
jenniwren
  • 341
  • 2
  • 10

2 Answers2

6

Sorry, may be a bit too late but I think this is the answer

Django support url param in admin. You can do like this

http://localhost/admin/app/model/add/?model_field=value

then you can add foreign key by replace model_field with your foreign key name and value with the ID. Hope this help someone else :D

James
  • 13,571
  • 6
  • 61
  • 83
  • 1
    You mean: `{% url admin:app_model_add %}?model_field={{myobject.id}}` ;) Indeed, useful if you control the place where this link gets created. – Tomasz Gandor Jan 03 '14 at 15:59
0

So you would prefer to have an additional button called "Add new Y" that inserts another row into Y with data from X?

Then override the save() method on X so it inserts a row into Y every time it gets called.

dan-klasson
  • 13,734
  • 14
  • 63
  • 101