这是我要在呈现 ModelForm (脆皮表单)时显示的布局。
用户选择父复选框(例如,位置1),但该复选框将自动选择子复选框,但用户可以取消选择某些复选框(例如,位置2)。
选择后,需要将与公告对象关联的设备ID 的值存储在数据库中。
models.py
class DeviceLocation(models.Model):
location_name = models.CharField(max_length=500)
def __str__(self):
return self.location_name
class Device(models.Model):
name = models.CharField(max_length=500)
location = models.ForeignKey(DeviceLocation, on_delete=models.CASCADE)
def __str__(self):
return self.name
class Announcement(models.Model):
submitted = models.ForeignKey(User, on_delete=models.CASCADE)
which_device= models.ForeignKey(Device, on_delete=models.CASCADE)
ad_name = models.CharField(max_length=500)
forms.py
class AnnouncementForm(forms.ModelForm):
which_device = forms.ModelMultipleChoiceField(
queryset=Device.objects.all(),
widget=forms.CheckboxSelectMultiple())
class Meta:
model = Announcement