Django — create a form with multiple select boxes?

Good day!
I need a page with a form where I can create a location that has a number of characteristics... and, for example, if I choose type of location, then I should be able to choose this location several types
I did something like this:

type_choices = (some choices)

class LocationType(models.Model):
name = models.CharField(max_length=50, verbose_name="Type of location", choices=type_choices)

class Location(models.Model):
types = models.ManyToManyField(LocationType, related_name='location_type')

class LocationForm(forms.ModelForm):
class Meta:
model = Location

but in my form I don't see Cheusov... i.e. the form is blank
July 9th 19 at 13:54
1 answer
July 9th 19 at 13:56
You need to create choices for location and then create a locations themselves. Or what's the problem?!
There is a Location object that has fields: name, ......, and type (at the same location may have a right to several types). I need to create a form where the user can create locations. In this case the form field Type should look like here calls a dropdown checkboxes (with, of course, multiple choice). Thank you! - Mackenzie.Pfannersti commented on July 9th 19 at 13:59
Well the campaign you need to create a custom form. In that context here calls a dropdown to insert all types created in the admin for example.
PS:Judging by the models you have in the admin to select the type of location of proposed options. - Ruben.Price56 commented on July 9th 19 at 14:02
: i.e. choices not needed here? create types through the admin panel? context this means using a queryset attribute? - Mackenzie.Pfannersti commented on July 9th 19 at 14:05
I mean in LocationForm define - types = forms.ModelMultipleChoiceField(queryset=LocationType.objects.all()) - Mackenzie.Pfannersti commented on July 9th 19 at 14:08
When renderes the page you need to add in the context of all types, to put them in dropbox as a choice, and pass the selected id type location. And create a record in the database.
choices get do not need. Look in the admin panel as shown. You just want it to do? - Ruben.Price56 commented on July 9th 19 at 14:11
Ah...OK.. thanks!.. and I thought that Jung has in the Arsenal some solution :) - Mackenzie.Pfannersti commented on July 9th 19 at 14:14
But not for that. But I do not understand what helped? - Ruben.Price56 commented on July 9th 19 at 14:17

Find more questions by tags Django