How to put an InlineFormSet into a ModelFormSet in Django? -
I ModelFormSet many forms through
would display where all associated with each form object InlineFormSets
displays in exchange for objects
Now I'm not really sure how to provide examples for each ModelFormSet
. I BaseModelFormSet
thought about subclassing, but I at all where to start and knowing that it is possible before that I have no clue on how to go through all the trouble.
Thanks in advance!
I have found an article that is focused on the exact problem. It works fine
For the sake of completeness I copied the code piece:
class blocks (models.Model): Description = model .CharField (MAX_LENGTH = 255) square building (models.Model): block = models.ForeignKey (block) address = models.CharField (MAX_LENGTH = 255) class tenant (models.Model): building = models.ForeignKey ( Building) name = models.CharField (MAX_LENGTH = 255) unit = models.CharField (MAX_LENGTH = 255) form the django.forms.models inlineformset_factory import, BaseInlineFormSet TenantFormset = inlineformset_factory (models.Building, models.Tenant, additional = 1) Class BaseBuildingFormset (BaseInlineFormSet): Def add_fields ( Self, form, index) :) Example = self.get_queryset ([: # Super-class fields as usual (BaseBuildingFormset, self) .add_fields (form, index) #Nested formset made to try to create index index] pk_value = instance.pk excluding index example = no pk_value = hash (form.prefix) # store .nested property formset form.nested = [TenantFormset (data = Selfkdata, for example = example, prefix = ' TENANTS_% s'% pk_value)] BuildingFormset = Inlineformset_factory (models.Block, models.Building, formset = BaseBuildingFormset, additional = 1)
Comments
Post a Comment