# Forms fields based on database content

**URL:** https://forum.djangoproject.com/t/forms-fields-based-on-database-content/8317
**Category:** Using Django
**Created:** [June 11, 2021, 6:15pm UTC](https://forum.djangoproject.com/t/forms-fields-based-on-database-content/8317 "2021-06-11T18:15:25Z")
**Posts on this page:** 1
**Showing post:** 3

<div class="post-metadata">

### Author: ![logikonabstractions](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/logikonabstractions/32/4066_2.png) [@logikonabstractions](https://forum.djangoproject.com/u/logikonabstractions)
#### Post date: [June 13, 2021, 3:42pm UTC](https://forum.djangoproject.com/t/forms-fields-based-on-database-content/8317/3 "2021-06-13T15:42:58Z")

</div>

Well, yes, there is that - e.g. the fk\_groupe. (And fk\_client I guess, but that’s dealt with when I save the form, formset.instance = self.object). But that’s not really the crux of the matter for me.

The crux of the matter is that each form instance in the formset corresponds to 1 entry in MetaGroup table (e.g. Plastic cable, LED lights, etc.). Thus that means that the fk\_groupe for each form in the formset must be set to the MetaGroup instance’s pk. So I guess that either in the template, or in an inclusion\_tag, or the form view, or perhaps class DiscountForm, I need to:

- Fetch the content of the MetaGroup table
- create/add a form to the formset corresponding each entry returned (LED Lights, Plastic cable, etc.)
- set the fk\_group for that form

But then, the inlineformset\_factory produces forms within a formset, but none of those forms will have any fk\_groupe assigned. So somehow I would have to assign them at some point, but I’m not sure what hook I have to actually do that. So my current approach would be to make that information (a list of fk\_groupe) available in the context to the template, and then assign it to each form instance as I iterate? (Or javascript would work & just .val() on each td).

I’m getting the pieces toghether to make that work. Thought it does seem complicated. I’m not really sure where I want to put the logic for that either.

---

_[View the full topic](https://forum.djangoproject.com/t/forms-fields-based-on-database-content/8317)._
