Referencing objects that don't exist that will be created in forms

14 views
Skip to first unread message

Chris Wedgwood

unread,
Oct 19, 2017, 5:23:36 AM10/19/17
to django...@googlegroups.com
Hi

I have the following scenario in my forms.py:

UNIVERSITY_CHOICES = University.objects.values_list('id', 'name')

class UniversityForm(forms.Form):
name = forms.CharField(widget=forms.Select(attrs={"class":
"selectpicker", "data-live-search": "true","title": "find
university..."},choices=UNIVERSITY_CHOICES), required=False)

The migration fails as it is expecting the university table to exist
but does not yet.
This is clearly an anti-pattern on my part. What is the correct way to
approach this?

thanks
Chris

Chris Wedgwood

unread,
Oct 19, 2017, 5:49:06 AM10/19/17
to Django users
Hi All

Ive overcome it by adding doing this:

def __init__(self, *args, **kwargs):
super(UniversityForm, self).__init__(*args, **kwargs)
self.fields['name'].choices = University.objects.values_list('id', 'name')

This makes sense. Don't reference objects that will be parsed before they exist 
Reply all
Reply to author
Forward
0 new messages