django - Using radio button control to select foreign key pointing to an item in nested inline formset -
i have following classes defined define node class. each node can have multiple nodeintf's assigned it. each nodeintf can have multiple nodeintfipaddr's assigned it. 1 of nodeintfipaddr's maybe assigned mgmt_ipaddr attribute on node object. , 1 of them maybe assigned mgmt_ipaddr_v6 attribute. in template, have nested table interfaces , want use radio button selector choose of ipaddrs selected mgmt_ipaddr(_v6) attributes on node object, i'm not quite sure how it. think that, iterate on ipaddr_formset, have check see if ipaddr represents selected mgmt_ipaddr, i'm not sure how that. appreciated.
class node(models.model): name = models.charfield(max_length=64, primary_key=true) mgmt_ipaddr = models.foreignkey('nodeintfipaddr', null=true, on_delete=models.set_null) mgmt_ipaddr_v6 = models.foreignkey('nodeintfipaddr', null=true, on_delete=models.set_null) class nodeintf(models.model): intf = models.charfield(max_length=32) node = models.foreignkey('node', on_delete=models.cascade) class meta: unique_together = ('node', 'intf') class nodeintfipaddr(models.model): node_intf = models.foreignkey('nodeintf', on_delete=models.cascade) ipaddr = inetaddressfield() class meta: unique_together = ('node_intf', 'ipaddr') class nodeform(modelform): class meta: model = node class nodeintfform(modelform): class meta: model = nodeintf class nodeintfipaddrform(modelform): class meta: model = nodeintfipaddr nodeintfipaddrformset = modelformset_factory(nodeintfipaddr, form=nodeintfipaddrform, extra=0) class basenodeintfformset(baseinlineformset): def add_fields(self, form, index): super(basenodeintfformset, self).add_fields(form, index) instance = self.get_queryset()[index] pk_value = instance.pk form.ipaddr_formset = nodeintfipaddrformset( queryset=nodeintfipaddr.objects.filter(node_intf=pk_value), prefix='intf_%s' % pk_value) nodeintfformset = inlineformset_factory(node, nodeintf, form=nodeintfform, formset=basenodeintfformset, extra=0) class nodeupdateview(updateview): form_class = nodeform model = node def get_context_data(self, **kwargs): c = super(nodeupdateview, self).get_context_data(**kwargs) node = self.get_object() c['action'] = reverse('node-update', kwargs={'pk': node.name}) if self.request.post: node_intfs = nodeintfformset(self.request.post, instance=node) if node_intfs.is_valid(): addrs = node_intfs.save_all() else: node_intfs = nodeintfformset(instance=node) c['node_intfs_formset'] = node_intfs return c
template snippet:
<table class='node_intfs'> <thead> <tr class='node_intf'> <th colspan='2'></th> <th>name</th> <th></th> </tr> <tr class='node_intf_ipaddr'> <th>ipv4 mgmt<br><label><input type='radio' name='mgmt_ipaddr' value=''{{ node.mgmt_ipaddr|yesno:', checked' }}>none</label></th> <th>ipv6 mgmt<br><label><input type='radio' name='mgmt_ipaddr_v6' value=''{{ node.mgmt_ipaddr_v6|yesno:', checked' }}>none</label></th> <th colspan='2'></th> </tr> </thead> <tbody> {% node_intf_form in node_intfs_formset %} <tr class='node_intf'> <td colspan='2'></td> <td>{{ node_intf_form.intf }}</td> <td></td> </tr> {% if node_intf_form.ipaddr_formset %} {% ipaddr_form in node_intf_form.ipaddr_formset %} <tr class='node_intf_ipaddr'> <td>todo</td> <---- these can't figure out <td>todo</td> <---- these can't figure out <td></td> <td>{{ ipaddr_form.ipaddr }}</td> </tr> {% endfor %} {% endif %} {% endfor %} </tbody> </table>
i able needed using following in template:
<td class='center'><input type='radio' name='mgmt_ipaddr' value='{{ ipaddr_form.instance.id }}'{% if node.mgmt_ipaddr_id == ipaddr_form.instance.id %} checked='checked'{% endif %}</td> <td class='center'><input type='radio' name='mgmt_ipaddr_v6' value='{{ ipaddr_form.instance.id }}'{% if node.mgmt_ipaddr_v6_id == ipaddr_form.instance.id %} checked='checked'{% endif %}</td>
this compares mgmt_ipaddr(_v6)_id node object id of instance tied individual ipaddr forms, accessible ipaddr_form.instance.id.
just completeness, missing management_form each of node_intf_forms , ipaddr_forms.
Comments
Post a Comment