hello, Im trying to setup a monitoring app, which has HostGroups and Hosts
its a ManyToMany relationships, ie
“Prod” hostgroup can include host1, host2, host3
“Webserver” hostgroup can include host1, host4, host5
so each host can be part of many Groups, and each Group can have many Hosts.
I setup my Model like this,
class Host(models.Model):
monit_id = models.UUIDField(primary_key=True, editable=False)
name = models.CharField(max_length=50, blank=True, null=True)
def __unicode__(self):
return self.name
class Meta:
constraints = [models.UniqueConstraint(fields=["monit_id"], name="unique_monit_id")]
class HostGroup(models.Model):
name = models.CharField(max_length=50, blank=False, null=False)
description = models.CharField(max_length=100, blank=True, null=True)
host = models.ManyToManyField(Host, blank=True)
def __unicode__(self):
return self.name
class Meta:
constraints = [models.UniqueConstraint(fields=["name"], name="unique_hostgroup_name")]
my Form is appending Host choices by doing a query on the Host object
class HostGroupForm(forms.ModelForm):
class Meta:
model = HostGroup
fields = "__all__"
name = forms.CharField(max_length=40, required=True)
description = forms.CharField(max_length=50, required=False)
host = forms.MultipleChoiceField(required=False, widget=forms.CheckboxSelectMultiple)
def __init__(self, *args, **kwargs):
super(HostGroupForm, self).__init__(*args, **kwargs)
self.fields['host'].choices = [(host.monit_id, host.name) for host in Host.objects.filter(approved=True)]
and finally my view saves the incoming form and creates a ManyToMany relationship in the M2M table that gets created automatically
if request.method == "POST":
obj = HostGroup.objects.get(pk=id)
form = HostGroupForm(request.POST, instance=obj)
if form.is_valid():
obj = form.save(commit=True)
for h in request.POST.getlist('host'):
obj.host.add(h)
I can see the relationship created from the obj.host.add(h) line
what I cant figure out is how to display the selected Hosts in the Group when the form is presented back to the template, right now it displays a blank list of all hosts from the query in the Form class,
how can i make it automatically “check” a checkbox if the host is in a Many2Many relationship with a HostGroup ?
Idea is to show the user all possible hosts that he can add to the HostGroup, and show which hosts are already part of this HostGroup (with a checkmark)
thanks