Hi,
On Dec/22/2020, Benny M wrote:
> That’s correct. I am using a class variable since there doesn’t seem
> to be a DRY - or at least not messy - way of manipulating this from
> __init__.
It's easy to make this mistake. I was bitten by something similar in a
class based view generating a queryset...
> That said, your explanation makes sense and does answer the “why” of
> this. If there’s a way of defining forms inside __init__ I haven’t
> found it… which is not to say that it doesn’t exist.
Yes, you can
> Here’s an example of what I’m referring to: (Pardon my poorly labeled
> model/form names)
>
> ```
> class M2MForm(forms.ModelForm):
> “”” Populate/update M2MModel name and m2m references ””"
> REF_OPTIONS = ((
r.id,
r.name) for r in RefModel.objects.all())
>
> ref_models = forms.MultipleChoiceField(
> widget=forms.SelectMultiple,
> choices=REF_OPTIONS,
> required=False
> )
>
> class Meta:
> model = M2MModel
> fields = ['name']
> ```
Try something like:
```
class M2MForm(forms.ModelForm):
def __init__(*args, **kwargs):
super().__init__(*args, **kwargs)
# At this point self.fields['name'] already exist...
# ...if you overwriteself.fields['name'] careful
# because you need to save it in save()
#
# self.fields gets created by the __init__() method of
# the parent class (actually grandparent I think :-) )
""" Populate/update M2MModel name and m2m references """
REF_OPTIONS = ((
r.id,
r.name) for r in RefModel.objects.all())
# this would add the field 'ref_models' in the form, but save()
# will not save. You can overwrite save and save it yourself
self.fields['ref_models'] = forms.MultipleChoiceField(
widget=forms.SelectMultiple,
choices=REF_OPTIONS,
required=False
)
class Meta:
model = M2MModel
fields = ['name']
```
I would look at not using forms.MultipleChoiceField and use
forms.ModelMultipleChoiceField(): then you should be able to pass
queryset (first argument for the ModelMultipleChoiceField) and avoid the
REF_OPTIONS=... line
Let me know what you find :-)
Cheers,
> To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/CH2PR14MB39136CDA719529325B91F0E3C0DF0%40CH2PR14MB3913.namprd14.prod.outlook.com.