Working with Models and Forms

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.

Note: When posting code here, please enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted.

Second note: I strongly encourage you to read the Django and Python coding conventions. What this particularly means here is that your models should be capitalized (e.g. AssignedUser insteand of assigneduser), and your fields should be consistently lower-case (e.g. serial_number instead of serialnumber). It’ll make your code a lot easier to read and understand. (You’ll also be glad you’ve done this when you come back to this code after a few months and need to remind yourself what this code is doing.)

Aside from the above, you haven’t quite yet posted enough information to fully diagnose this. For example, your get_company_choices method references a Profile model but you don’t show the Profile model here.

Finally, when you’re looking to select model instances for OneToMany or ManyToMany relationships, you’re probably more going to want to use the ModelChoiceField or the ModelMultipleChoiceField