AndrewIngram / django-extra-views

Django's class-based generic views are awesome, let's have more of them.
MIT License
1.38k stars 172 forks source link

Passing foreign key data to UpdateWithInlinesView forms via InlineFormSetFactory #244

Closed Maximost closed 2 years ago

Maximost commented 2 years ago

Good day.

I'm wondering if it's possible to pass foreign key object data to an UpdateWithInlinesView form via InlineFormSetFactory views?

Basically, I have a "grandparent" object that links to several "parent" objects (foreign key relation). Each parent object has relations to several child object types (each in their own table). What I'm hoping to do is to list those these child objects inline with the parents, which are themselves listed inline within the grandparent form. I should also mention that editing the child objects is not a requirement. I would simply like to show these as part of each editable parent object.

Something like this: Grandparent: GPtitleField, otherFields

I'm pretty familiar with how to pass context data to a basic create or update view. It also seems like I could pass a context query via the inline views but I don't see any mention of this sort of thing in the documentation.

class object_edit_inline(InlineFormSetFactory): form_class = ObjectGridEdit model = Object factory_kwargs = {'extra': 0, 'max_num': None, 'can_order': False, 'can_delete': False}

def get_formset_kwargs(self): kwargs = super().get_formset_kwargs() kwargs["context"] = self.object.children(filter on parent id) <--- something like this? return kwargs

Thank you so much for any insight you might have.

Maximost commented 2 years ago

Thank you for reviewing my question. I found another way to import inline object context data. Basically matching dictionary items via id, then interleaving those data within the form objects.