Model:
class SalesItem(models.Model):
item_description = models.CharField(max_length=40)
company = models.ForeignKey(Company)
Here I create a form with a dropdown, hoping to pass in the company as a source for the dropdown. Hold on to this thought, as I think that is not possible in my scenario.
Form:
class SalesItemFSForm(Form):
sales_item = forms.ModelChoiceField(required=False, queryset = '')
def __init__(self, company, *args, **kwargs):
super(SalesItemFSForm, self).__init__(*args, **kwargs)
self.fields.sales_item.queryset = company.salesitem_set.all()
Now within my view I would like to create a formset with this form:
formset_type = formset_factory(SalesItemFSForm, extra=0)
The problem becomes right away clear, as there seem to be no way that I could pass in the company to determine the source for the dropdown.
class DealType(models.Model):
deal_name = models.CharField(max_length=40)
sales_item = models.ManyToManyField(SalesItem)
price = models.DecimalField(decimal_places=2)

Many Thanks,
Houman
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/YGT3FYCdYbUJ.
class MyForm(forms.ModelForm):
categories = forms.Field(widget=DropDownMultiple)
def __init__(self, *args, **kwargs):
self.base_fields['categories'].widget.choices = Category.objects.values_list('id', 'name')
super(MyForm, self).__init__(*args, **kwargs)
categories = forms.Field(widget=DropDownMultiple)
to my form i get this exception:Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/decorators.py" in _wrapped_view
20. return view_func(request, *args, **kwargs)
File "/home/houman/projects/my/my_app/views.py" in deal_template_view
303. form = DealTypeForm(instance=deal)
File "/home/houman/projects/my/my_app/forms.py" in __init__
252. super(DealTypeForm, self).__init__(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in __init__
247. error_class, label_suffix, empty_permitted)
File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in __init__
95. self.fields = copy.deepcopy(self.base_fields)
File "/usr/lib/python2.7/copy.py" in deepcopy
174. y = copier(memo)
File "/usr/local/lib/python2.7/dist-packages/django/utils/datastructures.py" in __deepcopy__
129. for key, value in self.iteritems()])
File "/usr/lib/python2.7/copy.py" in deepcopy
174. y = copier(memo)
File "/usr/local/lib/python2.7/dist-packages/django/forms/fields.py" in __deepcopy__
180. result.widget = copy.deepcopy(self.widget, memo)
File "/usr/lib/python2.7/copy.py" in deepcopy
190. y = _reconstruct(x, rv, 1, memo)
File "/usr/lib/python2.7/copy.py" in _reconstruct
334. state = deepcopy(state, memo)
File "/usr/lib/python2.7/copy.py" in deepcopy
163. y = copier(x, memo)
File "/usr/lib/python2.7/copy.py" in _deepcopy_dict
257. y[deepcopy(key, memo)] = deepcopy(value, memo)
File "/usr/lib/python2.7/copy.py" in deepcopy
163. y = copier(x, memo)
File "/usr/lib/python2.7/copy.py" in _deepcopy_dict
257. y[deepcopy(key, memo)] = deepcopy(value, memo)
File "/usr/lib/python2.7/copy.py" in deepcopy
190. y = _reconstruct(x, rv, 1, memo)
File "/usr/lib/python2.7/copy.py" in _reconstruct
329. y = callable(*args)
File "/usr/lib/python2.7/copy_reg.py" in __newobj__
93. return cls.__new__(cls, *args)
Exception Type: TypeError at /deal/add/
Exception Value: object.__new__(NotImplementedType) is not safe, use NotImplementedType.__new__()
DropDownMultiple widget http://djangosnippets.org/snippets/747/
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.