I have not got a response back from the Django admins regarding my blocked first response to your post. So I’m posting over again.
I’m not using forms.RadioSelect for my multi select widgets. I’m using forms.CheckboxSelectMultiple with models.ManyToManyField(SubwayVeggie). I assume you mean tuples when you say “two column” select. I’m using those for other fields and they work fine for small lists. But I need to use the ManyToMay for larger lists and be able to maintain the list in the admin section. However I can’t find any documentation that explains what I’m looking to do.
-
Show the multiple selections in the subwayOrder. In the veggies field see a comma separated string of the values selected in the veggies column.
-
Show the values in the admin section using radio buttons rather than the default list.
-
Have the values selected be posted with the rest of the selections. I can do this with single selections just not with multiple.
@login_required(login_url='/login')
def subway_menu(request):
# First time user gets here submitted is False
submitted = False
if request.method == "POST":
form = SubwayForm(request.POST)
if form.is_valid():
# form.save() standard way.
instance = form.save(commit=False)
# Attach user_name to instance to store the user submitting the order.
instance.user_name = request.user
# user_name = request.user
print(instance.user_name)
# Save meal to Database with user_name
instance.save()
return HttpResponseRedirect('/meals/subway_menu?submitted=True')
else:
# Define the for for the render below.
form = SubwayForm
if 'submitted' in request.GET:
submitted = True
return render(request, 'meals/subway_menu.html', {'form': form, 'submitted': submitted})
models.py
# ───────────────────────────────────────────────────────────────────────────────
# Subway Subway Orders
# ───────────────────────────────────────────────────────────────────────────────
class SubwayOrder(models.Model):
# first_name = User.first_name
user_name = models.ForeignKey(User,
on_delete=models.CASCADE,
default=None,
null=True,
blank=True)
bread = models.ForeignKey(SubwayBread, default=13, on_delete=models.CASCADE)
order_Submitted_Date = models.DateTimeField(auto_now=True)
sandwich = models.ForeignKey(SubwaySandwich, default=0, on_delete=models.CASCADE)
BREAD_SIZE = (
('12"','12"'),
('6"','6"'),
)
bread_size = models.CharField(max_length=10, choices=BREAD_SIZE, default='12"')
TOASTED = (
('Yes', 'Yes'),
('No', 'No'),
)
toasted = models.CharField(max_length=10, choices=TOASTED, default='No')
# cheese references the CharField cheese above.
# null = False (default) NO empty values allowed.
# blank = False (default) NO blank values allowed.
# required = True (default)
cheese = models.ManyToManyField(SubwayCheese)
# cheese = models.ForeignKey(SubwayCheese, default=1, on_delete=models.CASCADE)
# extra = models.ManyToManyField(SubwayExtra)
veggie = models.ManyToManyField(SubwayVeggie)
# veggie = models.ForeignKey(SubwayVeggie, default=1, on_delete=models.CASCADE)
# sauce = models.ForeignKey(SubwaySauce, default=1, on_delete=models.CASCADE)
# drink = models.ForeignKey(SubwayDrink, default=1, on_delete=models.CASCADE)
# cookie = models.ForeignKey(SubwayCookie, default=1, on_delete=models.CASCADE)
# chips = models.ForeignKey(SubwayChip, default=1, on_delete=models.CASCADE)
# Don't know what the hell to return here.
def __int__(self):
return self.bread
# ───────────────────────────────────────────────────────────────────────────────
admin.py
# ───────────────────────────────────────────────────────────────────────────────
# Subway Orders
# ───────────────────────────────────────────────────────────────────────────────
@admin.register(SubwayOrder)
class SubwayOrder(admin.ModelAdmin):
list_display = (
'user_name',
# 'first_name',
'bread',
'order_Submitted_Date',
'sandwich',
'toasted',
'bread_size',
# 'cheese',
# 'extra',
# 'veggie', # Required a custom method.
# 'sauce',
# 'drink',
# 'cookie',
# 'chips',
)
# ordering = ('bread_size',)
# search_fields = ('toasted',)
radio_fields = {
'bread':admin.VERTICAL,
'sandwich':admin.VERTICAL,
'toasted':admin.VERTICAL,
# 'cheese':admin.VERTICAL,
'bread_size':admin.VERTICAL,
# 'extra':admin.VERTICAL,
# 'veggie':admin.VERTICAL,
}
Best regards,
-=Mike=-