Select2 default value

70 views
Skip to first unread message

matteo gassend

unread,
Aug 13, 2018, 10:48:38 AM8/13/18
to Django users
Hey everyone,

I am trying to set a default value for a Select2Widget(from django_select2) to use in a formset.

I am able to set one for those added with js, but cannot set the one for the form already present.

This is the form

class Ddt_in_itemForm(forms.ModelForm):
class Meta:
model = ddt_in_item
fields = ("prodotti_var", "quantita", "scade")
widgets = {
'prodotti_var': Select2Widget()
}
def __init__(self, *args, **kwargs):
super(Ddt_in_itemForm, self).__init__(*args, **kwargs)
self.fields["scade"].required = False
self.fields["prodotti_var"].queryset = prodotti.objects.filter(is_active=True).order_by('nome_prodotto')

Can you guys help me?

Matthew Pava

unread,
Aug 13, 2018, 11:08:35 AM8/13/18
to django...@googlegroups.com

In Django forms, it’s called an initial value instead of a default value.

 

https://docs.djangoproject.com/en/2.1/ref/forms/api/#django.forms.Form.initial

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/83b07178-66e1-419a-8f02-f7769db6d3f2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

matteo gassend

unread,
Aug 13, 2018, 11:10:29 AM8/13/18
to Django users
Even for a Modelform?

To post to this group, send email to djang...@googlegroups.com.

Matthew Pava

unread,
Aug 13, 2018, 11:18:13 AM8/13/18
to django...@googlegroups.com

Yes…

To post to this group, send email to django...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages