Django forms - Select a valid choice. That choice is not one of the available choices -
in form have select box when after submit getting error on field: "select valid choice. choice not 1 of available choices." i'm using code in other form , there works fine not in form :(
forms.py:
blank_choice = (('', '---------'),) class clientcreateform(forms.modelform): class meta: model = client fields = ('name', 'address', 'zip_code', 'city', 'country', 'forwarding_address', 'forwarding_zip_code', 'forwarding_city', 'forwarding_country') def __init__(self, *args, **kwargs): super(clientcreateform, self).__init__(*args, **kwargs) self.fields['country'].choices = countriesshortcut.objects.all().values_list('id', 'name') self.fields['forwarding_country'].choices = blank_choice + tuple( countriesshortcut.objects.all().values_list('id', 'name'))
models.py
class countriesshortcut(models.model): name = models.charfield(max_length=80, unique=true) class meta: ordering = ['id'] def __init__(self): self.code class client(models.model): id = models.onetoonefield(user, on_delete=models.cascade, unique=true, primary_key=true) name = models.charfield(max_length=256, unique=true) address = models.charfield(max_length=64) zip_code = models.charfield(max_length=10, help_text='zip code') city = models.charfield(max_length=64) country = models.foreignkey(countriesshortcut, related_name='country', blank=true, null=true) forwarding_address = models.charfield(max_length=64, blank=true) forwarding_zip_code = models.charfield(max_length=10, blank=true) forwarding_city = models.charfield(max_length=64, blank=true) forwarding_country = models.foreignkey(countriesshortcut, related_name='forwarding_country', blank=true, null=true) def __str__(self): re = self.name + ' [' + str(self.id) + ']' return re
can give me advice?
traceback showed after changed queryset:
environment: request method: request url: http://127.0.0.1:8000/panel/client/create/ django version: 1.8.8 python version: 3.5.1 installed applications: ('django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'core', 'client', 'registration', 'avatar', 'filer', 'mptt', 'easy_thumbnails', 'reversion') installed middleware: ('django.contrib.sessions.middleware.sessionmiddleware', 'django.middleware.common.commonmiddleware', 'django.middleware.csrf.csrfviewmiddleware', 'django.contrib.auth.middleware.authenticationmiddleware', 'django.contrib.auth.middleware.sessionauthenticationmiddleware', 'django.contrib.messages.middleware.messagemiddleware', 'django.middleware.clickjacking.xframeoptionsmiddleware', 'django.middleware.security.securitymiddleware') template error: in template c:\users\loc\pycharmprojects\crm\templates\panel\client\form.html, error @ line 131 __init__() takes 1 positional argument 3 given 121 : <div class="col-sm-3"> 122 : {{ form_client.city }} 123 : {% if form_client.errors.city %} 124 : <p class="help-block"> 125 : {{ form_client.errors.city }} 126 : {{ form_client.non_field_errors.city }} 127 : </p> 128 : {% endif %} 129 : </div> 130 : <div class="col-sm-3"> 131 : {{ form_client.country }} 132 : {% if form_client.errors.country %} 133 : <p class="help-block"> 134 : {{ form_client.errors.country }} 135 : {{ form_client.non_field_errors.country }} 136 : </p> 137 : {% endif %} 138 : </div> 139 : </div> 140 : </div> 141 : </div> traceback: file "c:\users\loc\djangoenvironment\lib\site-packages\django\core\handlers\base.py" in get_response 132. response = wrapped_callback(request, *callback_args, **callback_kwargs) file "c:\users\loc\djangoenvironment\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view 22. return view_func(request, *args, **kwargs) file "c:\users\loc\pycharmprojects\crm\core\views.py" in client_create 111. return render(request, 'panel/client/form.html', dict) file "c:\users\loc\djangoenvironment\lib\site-packages\django\shortcuts.py" in render 67. template_name, context, request=request, using=using) file "c:\users\loc\djangoenvironment\lib\site-packages\django\template\loader.py" in render_to_string 99. return template.render(context, request) file "c:\users\loc\djangoenvironment\lib\site-packages\django\template\backends\django.py" in render 74. return self.template.render(context) file "c:\users\loc\djangoenvironment\lib\site-packages\django\template\base.py" in render 210. return self._render(context) file "c:\users\loc\djangoenvironment\lib\site-packages\django\template\base.py" in _render 202. return self.nodelist.render(context) file "c:\users\loc\djangoenvironment\lib\site-packages\django\template\base.py" in render 905. bit = self.render_node(node, context) file "c:\users\loc\djangoenvironment\lib\site-packages\django\template\debug.py" in render_node 79. return node.render(context) file "c:\users\loc\djangoenvironment\lib\site-packages\django\template\loader_tags.py" in render 135. return compiled_parent._render(context) file "c:\users\loc\djangoenvironment\lib\site-packages\django\template\base.py" in _render 202. return self.nodelist.render(context) file "c:\users\loc\djangoenvironment\lib\site-packages\django\template\base.py" in render 905. bit = self.render_node(node, context) file "c:\users\loc\djangoenvironment\lib\site-packages\django\template\debug.py" in render_node 79. return node.render(context) file "c:\users\loc\djangoenvironment\lib\site-packages\django\template\loader_tags.py" in render 65. result = block.nodelist.render(context) file "c:\users\loc\djangoenvironment\lib\site-packages\django\template\base.py" in render 905. bit = self.render_node(node, context) file "c:\users\loc\djangoenvironment\lib\site-packages\django\template\debug.py" in render_node 79. return node.render(context) file "c:\users\loc\djangoenvironment\lib\site-packages\django\template\debug.py" in render 92. output = force_text(output) file "c:\users\loc\djangoenvironment\lib\site-packages\django\utils\encoding.py" in force_text 90. s = six.text_type(s) file "c:\users\loc\djangoenvironment\lib\site-packages\django\utils\html.py" in <lambda> 399. klass.__str__ = lambda self: mark_safe(klass_str(self)) file "c:\users\loc\djangoenvironment\lib\site-packages\django\forms\forms.py" in __str__ 537. return self.as_widget() file "c:\users\loc\djangoenvironment\lib\site-packages\django\forms\forms.py" in as_widget 593. return force_text(widget.render(name, self.value(), attrs=attrs)) file "c:\users\loc\djangoenvironment\lib\site-packages\django\forms\widgets.py" in render 513. options = self.render_options(choices, [value]) file "c:\users\loc\djangoenvironment\lib\site-packages\django\forms\widgets.py" in render_options 539. option_value, option_label in chain(self.choices, choices): file "c:\users\loc\djangoenvironment\lib\site-packages\django\forms\models.py" in __iter__ 1107. obj in queryset: file "c:\users\loc\djangoenvironment\lib\site-packages\django\db\models\query.py" in iterator 255. obj = model_cls.from_db(db, init_list, row[model_fields_start:model_fields_end]) file "c:\users\loc\djangoenvironment\lib\site-packages\django\db\models\base.py" in from_db 489. new = cls(*values) exception type: typeerror @ /panel/client/create/ exception value: __init__() takes 1 positional argument 3 given
your fields foreign keys. default django sets available values fields in forms objects of model. looking @ code, think remove clientcreateform
__init__
completely.
if still want set custom choices, should not set choices
attribute. should use queryset
:
class clientcreateform(forms.modelform): # meta goes here def __init__(self, *args, **kwargs): super(clientcreateform, self).__init__(*args, **kwargs) self.fields['country'].queryset = countriesshortcut.objects.all() self.fields['forwarding_country'].queryset = countriesshortcut.objects.all()
your fields marked null=true
, empty dash added automatically.
choices
should used if have non-relationship field, example, charfield
choices attribute set.
Comments
Post a Comment