python - Pass choices from views to form -


i use form bannerform create new banner object trough add_banner views (and template). use class options definite affiliation objects permit in form bannerform (affiliation field).

i'm trying pass choices views form give me valueerror: many values unpack (expected 2)

my code worked when new_affiliation foreignkey need more values. think must definite 'choices' in views or have problem on first migration (it appear call database tables models.py not views.py, if put options.objects.get(id=1) on models.py give error because tables don't exist yet).

my form.py:

from django import forms core.models import options, banner, affiliation #somethings other  class bannerform(forms.modelform):     name = forms.charfield(max_length=32)     affiliation = forms.choicefield('choices')     #affiliation = forms.modelchoicefield('choices') #same error     class meta:         model = banner         exclude = (#some fields) 

my models.py:

from django.db import models django.contrib.auth.models import user django import forms  class options(models.model):     new_affiliation = models.manytomanyfield('affiliation')     #new_affiliation = models.foreignkey('affiliation') #this worked (with changes in views)  class affiliation(models.model):     name = models.charfield(max_length=32, unique=true)  class banner(models.model):     name = models.charfield(max_length=32, unique=true)     affiliation = models.foreignkey(affiliation) 

my views.py:

def add_banner(request):     if request.method == 'post':         #some code here     else:         options = options.objects.get(id=1)         print(options.new_affiliation.all()) #controll         choices = options.new_affiliation.all()         print(choices) #controll         form = bannerform(choices, initial={                         #some code regarding other fields             })            return render(request, 'core/add_banner.html', {'form': form}) 

my add_banner.html:

<form role="form" id="banner_form" enctype="multipart/form-data "method="post" action="../add_banner/">    {% csrf_token %}   {% hidden in form.hidden_fields %}   {{ hidden }}   {% endfor %}    {% field in form.visible_fields %}     {{ field.errors }}     {{ field.label }}     {{ field }}           {{ field.help_text }}     <br />   {% endfor %} 

any apreciated.

updated. changed views.py:

def add_banner(request):     if request.method == 'post':         #some code here     else:         options = options.objects.get(id=1)         print(options.new_affiliation.all()) #controll         choices = tuple(options.new_affiliation.all())         print(choices) #controll         form = bannerform(choices, initial={                         #some code regarding other fields             })            return render(request, 'core/add_banner.html', {'form': form}) 

but still give error.

update 2. if pass choices directly form.py works: views.py:

def add_banner(request):     if request.method == 'post':         #some code here     else:         form = bannerform(request.post or none, initial={             #some code regarding other fields             })     return render(request, 'core/add_banner.html', {'form': form}) 

my forms.py:

class bannerform(forms.modelform):     options = options.objects.get(id=1)     choices = options.new_affiliation.all()     name = forms.charfield(max_length=32)     affiliation = forms.modelchoicefield(choices) 

unluckly give problems on first migration (see above).

i'm trying pass choices using init method...

my forms.py:

class bannerform(forms.modelform):     name = forms.charfield(max_length=32)     affiliation = forms.modelchoicefield(choices)      def __init__(self, *args, **kwargs):         options = options.objects.get(id=1)         choices = options.new_affiliation.all()         #choices = kwargs.pop('choices')         super(regentform, self).__init__(*args, **kwargs)         self.fields['affiliation'] = choices 

but choices not definite

from can see here, looks getting error "too many values unpack" because not sending "choices" correct type. choicefield takes choices tuple, seen in the documentation models. if looking define choices based on queryset, you'll have convert tuple can interpreted valid "choices". in 1 of projects example, needed prepare set of years tuple allow users select list of pre-determined years. specified following function this:

def years():     response = []     = datetime.utcnow()     in range(1900, now.year + 1):         response.append([i, str(i)])     return tuple(response) 

since tuples meant immutable, isn't idea cast them, based on principle. however, in case seems necessary measure declare okay possible variability these statements.

in specific situation, might consider doing this:

choices = tuple(options.new_affiliation.all().values()) 

i have not tested code, , frankly not familiar project , may mistaken in part of response. result, may require further tweaking, give try. based on error, program breaking currently. update here if make progress.


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -