19👍
You’re getting a flat value_list back which will just be a list of the ids, but when you do that, you’re probably better off using a plain ChoiceField
instead of a ModelChoiceField
and providing it with a list of tuples, not just ids. For example:
class CommandSubmitForm(ModelForm):
iquery = LiveDataFeed.objects.values_list('unit_id', flat=True).distinct()
iquery_choices = [('', 'None')] + [(id, id) for id in iquery]
unit_id = forms.ChoiceField(iquery_choices,
required=False, widget=forms.Select())
You could also leave it as a ModelChoiceField
, and use LiveDataFeed.objects.all()
as the queryset, but in order to display the id in the box as well as have it populate for the option values, you’d have to subclass ModelChoiceField
to override the label_from_instance
method. You can see an example in the docs here.
8👍
Before you call form.is_valid()
, do the following:
-
unit_id = request.POST.get('unit_id')
-
form.fields['unit_id'].choices = [(unit_id, unit_id)]
Now you can call form.is_valid()
and your form will validate correctly.
- How to display "This many months ago" in Django using Humanize?
- Django raises MultiValueDictKeyError in File Upload
- Calculate point based on distance and direction
0👍
this is an old question but i faced the same problem, the solution according the django docs is to add to_field_name="name" where name is the column you want to show on the choices
iquery = LiveDataFeed.objects.values_list('unit_id', flat=True).distinct()
unit_id = forms.ModelChoiceField(queryset=iquery, empty_label='None',
required=False, widget=forms.Select(),to_field_name="unit_id")
- Django + Forms: Dynamic choices for ChoiceField
- How to filter filter_horizontal in Django admin?
- Django: values_list() multiple fields concatenated
- How to output text from database with line breaks in a django template?
- Use a django built in filter in code (outside of a template)
0👍
Rewrite the _init function. Use Limited_condition variable that delete this field in update view. in my case ‘author’ field caused problem ‘Select a valid choice’. I just removed it in initiation of the form.
forms.py
class UrlentryForm(ModelForm):
class Meta:
model = Urlentry
fields = ['url_text', 'author', 'url_id', 'datetime_available_from', 'datetime_available_to', 'partner_ads','qr_code','snapshot']
def __init__(self, *args, **kwargs):
limited_condition = kwargs.pop('limited_condition', None)
super(UrlentryForm, self).__init__(*args, **kwargs)
if limited_condition:
del self.fields['author']
self.fields['url_id'].widget = HiddenInput()
self.fields['url_id'].required = False
self.fields['qr_code'].widget = HiddenInput()
self.fields['qr_code'].required = False
self.fields['snapshot'].widget = HiddenInput()
self.fields['snapshot'].required = False
Then modify views.py
views.py
@login_required(login_url=reverse_lazy('auth:login'))
def update_urlentry(request, pk):
#....
if request.method == "POST":
urlentry_form = UrlentryForm(request.POST, instance=urlentry, limited_condition=True)
if urlentry_form.is_valid():
urlentry = urlentry_form.save(commit=False)
# additional modification of urlentry, if needed
urlentry.save()
else:
messages.error(request, urlentry_form.errors)
redirect('polls:detail', pk=urlentry.pk)
#and finalizing code follows here
0👍
What worked for me is populating the choices to the form before calling the is_valid()
method.
form = CommandSubmitForm(request.POST)
form.fields["unit_id"].choices = choices
if form.is_valid():
# add your logic here
- How to upgrade Django on ubuntu?
- Select Children of an Object With ForeignKey in Django?
- Django queryset __contains case sensitive?
- How to make follower-following system with django model