Instead off writing an __init__ for the form class, I ended up with this
get_form()
in my view class:
def get_form(self, form_class=None):
form = self.get_form_class()
pk = self.request.user.pk
self.object = Account.objects.get(pk=pk)
form_instance = form(instance=self.object)
self.filter_modelChoices(form_instance)
return form_instance
def filter_modelChoices(self, form_instance):
"""
Changes the model choice fields to be correctly filtered
as required by request.user.
"""
opts = self.model._meta
for formfield_name in form_instance.fields:
formfield = form_instance.fields[formfield_name]
if isinstance(formfield, ModelChoiceField):
[modelfield] =
[f for f in opts.fields if f.name == formfield_name]
qs = userVisibleFilterQS(
self.request.user.pk,
False,
modelfield.related_model())
if qs:
formfield.queryset = formfield.queryset.filter(qs)
Am 16.11.2015 um 18:38 schrieb Axel Rau <[email protected]>:
> Any idea how to add __init__() to a form class, created by modelform_factory
> () ?
Axel
---
PGP-Key:29E99DD6 ☀ +49 160 9945 7889 ☀ computing @ chaos claudius
--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/30621D2F-064C-4734-8669-FECACD1F3083%40Chaos1.DE.
For more options, visit https://groups.google.com/d/optout.