class SClients(models.Model): swname = models.ForeignKey(s_switch,verbose_name="SWITCH") vlan = models.ForeignKey(s_vlancl,verbose_name="VLAN") class s_switch(models.Model): sw_name = models.CharField(max_length=50,verbose_name="SW_NAME",unique=True) vlan_cl = models.ManyToManyField(s_vlancl,verbose_name='VLAN_CL') 

How in admin-ke to fill the field based on the selected value, for example: choose swname from the s_switch table, and the vlan field should be selected from the filled s_switch data ( vlan_cl field)

    2 answers 2

    If I understand correctly, then you need to dynamically change the values ​​for selection in the vlan drop-down list when choosing from the drop-down list swname. In this case, you need to hang the handler on the swname list in js-code, when the value changes in it, you need to send ajax request to the server, filter the necessary data from s_vlancl something like this: vlans = s_vlancl.objects.filter(s_switch=s_switch) , return them to js and create the desired dropdown list on the page. See also this question.

    • It is clear to learn js in general. - avdoshkin
    • In general, yes, but with jQuery it's all more or less simple, and since the task is quite typical you can find many examples on the Internet. On the server side, you just need to write a view, which will, on the basis of the passed parameter, return xml with the necessary data from the database. Then just add the URL and refer to it from the js-script. - l0ki

    Dig in the direction of ForeignKey