python - Set initial value for dynamically created ModelChoiceField in Class Based View -


i'm creating form has modelchoicefield. i'm able set values field follows:

class deviceform(form):     devices = modelchoicefield(queryset=none, widget=radioselect, empty_label=none)      def __init__(self, *args, **kwargs):         super(deviceform, self).__init__(*args, **kwargs)         user_id = kwargs['initial']['user_id']         devices = device.objects.filter(user_id=user_id, is_validated=true)         self.fields['devices'].queryset = devices 

this works perfectly, can't seem able set initial value. i've tried multiple things such adding initial value get_initial():

def get_initial(self):     initial = super(deviceview, self).get_initial()     initial['user_id'] = self.kwargs['user_id']     default_device = device.objects.get(user_id=self.kwargs['user_id'], is_validated=true, is_default=true)     initial['devices'] = default_device.id     return initial 

but doesn't work. i've come across multiple questions on stackoverflow instantiate form , pass in initial value so:

#get default device form = deviceform(initial={'devices': default_device.id}) 

but ideally avoid it, since i'm trying stick django generic base views (which this, , don't allow initial kwarg far i'm aware):

def get(self, request, user_id, **kwargs):     # stuff      form = self.get_form()     return self.render_to_response(self.get_context_data(form=form)) 

setting devices in get_initial() should work, since self.get_form_kwargs() returns:

{'prefix': none, 'initial': {'user_id': u'1', 'devices': 42}} 

or, depending on i'm supposed choose:

{'prefix': none, 'initial': {'user_id': u'1', 'devices': <device: device object>}} 

doing following still doesn't work (basically the same thing get_form() manually).

def get(self, request, user_id, **kwargs):     #do stuff     form_class = self.get_form_class()     form = form_class(**self.get_form_kwargs())     return self.render_to_response(self.get_context_data(form=form)) 

setting initial value in constructor of form doesn't work:

class deviceform(form):     devices = modelchoicefield(queryset=none, widget=radioselect, empty_label=none)      def __init__(self, *args, **kwargs):         super(deviceform, self).__init__(*args, **kwargs)         user_id = kwargs['initial']['user_id']         devices = device.objects.filter(user_id=user_id, is_validated=true)         self.fields['devices'].queryset = devices         default_device = devices.filter(is_default=true)         self.fields['devices'].initial = default_device  # or default_device.id 

does know of way accomplish this?

you need use suggested way assign initial, it's still available in class based views. quoting django doc:

class myformview(view):     form_class = myform     initial = {'key': 'value'}     template_name = 'form_template.html'      def get(self, request, *args, **kwargs):         form = self.form_class(initial=self.initial)         return render(request, self.template_name, {'form': form}) 

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

css - Can I use the :after pseudo-element on an input field? -