Adding default rows to Django admin inlines

29 views
Skip to first unread message

Kevin Olbrich

unread,
Jan 18, 2019, 12:07:29 PM1/18/19
to Django users
Hi!

I have a question regarding inlines in the Django admin interface.

Currently there are following models:
Jobs - holds the Job-ID, an action (FK JobActions) and execution status
JobActions - holds a list of possible actions (like "create a new customer") [Also has a ManyToMany against JobAttributes - see below]
JobAttributes - A list of attributes that are possible
JobOptions - values for attributes (FK JobAttributes) for a job (FK Jobs)
JobLog - unlimited rows of text-logs (in this case, API dumps), FK Jobs (not of interest here)

An action can be "add new customer". This action needs an attribute "customer name".

This scenario works perfectly, I can create new Jobs that have attributes but I need to add new lines in the inline part of the admin form.
I have added a ManyToMany-field from JobActions to JobAttributes. This gives me a list which action needs which attribute in JobAttributes.

Is it possbile to pre-fill the inline-part of the form with the attributes? This would be much easier for input.
Currently there are no rows, I need to add a new line and choose the attribute for which I would like to add a value.

Think of it like a dynamic (support) contact form where you get different inputs if you choose a different topic.

Is this possible with the admin? The relation (which JobActions needs which JobAttributes) is available.

Thank you very much.

Kevin

Mohammad Etemaddar

unread,
Jan 19, 2019, 3:47:45 PM1/19/19
to Django users
If I get you right you need to add m2m objects with a default value.
In this situation, you can create an intermediate model to build m2m.
For example:
JobActionRel:
job = fk
action = fk-to-actions (default=Action.objects.get...)

And use it as through model:
https://docs.djangoproject.com/en/2.1/topics/db/models/#extra-fields-on-many-to-many-relationships

Now, in admin you see the default action.
Remember, before making it through, create JobActionRels from previous m2m objects.

Kevin Olbrich

unread,
Jan 28, 2019, 6:02:15 AM1/28/19
to Django users
Hi!

Maybe I did not fully understand but it seems we indeed don't talk about the same.
I already got M2M relation.
If I access the "Jobs" admin-page, I have the option to add inline-options.
On a new object, there are no lines but I can add new.
I would like to add lines for the attributes from M2M relation of (Actions -> Attributes).
Every action has a different set of attributes that needs to be defined. If I open the admin page for Jobs, it would need to refresh (or AJAX) the form to display the correct default set of attributes in the inline area, as soon as I choose / change the Action type.

Pseudo: With HTML and JS I would listen for a change event, then AJAX an API for the attributes. Then I would clear the inline-area and add new rows for each mandatory attribute.

I wonder if this is possible with Django alone, without building a view.

Kind regards
Kevin
Reply all
Reply to author
Forward
0 new messages