Fairly new to Django and I have a complex question.
When users have an account, they can logon and I’ve created the user.profile model with many fields to include company and address. I have a separate django app called managerboard and it has devices with: deviceinfo model, modelType, serialnumber, macid, manufacturer, assignedcompany, assigneduser.
class assigneduser(models.Model):
user = models.ForeignKey(Profile, on_delete=models.CASCADE)
class deviceinfo(models.Model):
Modeltype = (
(‘LTS-100’, ‘LTS-100’),
(‘LTS-200’, ‘LTS-200’)
)
Manufacturer = (
(‘Dell’, ‘Dell’),
(‘HP’, ‘HP’),
(‘Lenovo’, ‘Lenovo’)
)
modeltype = models.CharField(max_length=100, choices=Modeltype)
serialnumber = models.CharField(max_length=15)
macid = models.CharField(max_length=50)
manufacturer = models.CharField(max_length=50, choices=Manufacturer)
assignedcompany = models.CharField(max_length=50, default=‘None’)
assigneduser = models.ManyToManyField(assigneduser)
def __str__(self):
return '{} - {}'.format(self.serialnumber, self.modeltype)
In managerboard.forms I’ve created the following
def get_company_choices():
companies = Profile.objects.order_by(‘company’).values_list(‘company’, flat=True).distinct()
choices = [(c, c) for c in companies]
return choices
class deviceinfoForm(forms.ModelForm):
#list has to look like this belove
class Meta:
model = deviceinfo
fields = ['manufacturer', 'modeltype', 'serialnumber', 'macid', 'assignedcompany', 'assigneduser']
widgets = {
'assignedcompany':forms.Select(choices=get_company_choices())
}
class updatedeviceForm(forms.ModelForm):
class Meta:
model = deviceinfo
fields = [‘modeltype’,‘serialnumber’, ‘macid’, ‘manufacturer’, ‘assignedcompany’, ‘assigneduser’]
widgets = {
‘assignedcompany’:forms.Select(choices=get_company_choices())
}
I would like the assigneduser to have a manytomany relationships because In the future a device assigned to a company may have multiple admins. When I include the manytomany relationships into the form, nothing populates. Any help would be greatly appreciated it.