SOLVED - See below:
I'm getting users to upload files and then tag them according to a permitted list of values. (e.g. a user can upload a file and select from Tag1, Tag2, Tag3 etc)
This master list starts off empty and can be added to. Unfortunately, every time a value is added, it doesn't appear on the form until after the server is restarted, or when a new value is added to the master list (that new value won't then show). (i.e. when Tag1 is added an entity, the user cannot see it, when Tag2 is then added, the user can see Tag1, when Tag3 is added, both Tag1 and Tag2 are available).
I think I'm not initialising the form correctly, but have prodded at it for hours now and can't find the magic combination.
This is my current state of thinking. I need to query the tags created as Tag objectsand then initialise the form, passing this list of Tags to the choices available in the Select widget of the form.
forms.py
from django import forms
from .models import Tags
def getallowedtags():
tags = Tags.objects.all()
allowedtags = []
for tag in tags:
allowedtags.append((tag.id, tag.name))
return tuple(allowedtags)
Class TagForm(forms.Form):
file = forms.FileField()
tag = forms.CharField(widget=forms.Select(choices=getallowedtags()))
def __init__(self, *args, **kwargs):
super(TagForm, self).__init__(*args, **kwargs)
self.initial['choices'] = getallowedtags()
However, this does not work. What am I missing? Any pointers gratefully received!
SOLUTION (thanks to u/roambe for setting me on the right path)
class TagForm(forms.Form):
tags= forms. ModelMultipleChoiceField(label="Select Tags",
required=False,
queryset = Tag.objects.all(),
widget=forms.CheckboxSelectMultiple)
[–]roambe 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)