I have a Django form that has a select field to choose a User for a ForeignKey relationship. Initially when the page loads it displays the select field with all Users as options. When refreshing the page (or reloading as a result of validation errors) the select field has no options. Continuing to refresh loads them sometimes but not consistently.
forms.py:
user_choices = ((obj.id, obj.email) for obj in User.objects.all())
class EditContentForm(forms.ModelForm):
class Meta:
model = Content
fields = ['title', 'user']
def __init__(self, *args, **kwargs):
super(EditContentForm, self).__init__(*args, **kwargs)
self.fields['title'].widget = TextInput(attrs = {'class' : 'form-control'})
self.fields['user'].widget = Select(attrs = {'class' : 'form-control'}, choices = user_choices)
I've used Django forms to do this many times and never seen this issue. Any ideas are appreciated, thank you.
Aucun commentaire:
Enregistrer un commentaire