Difficult to subclass FieldBase for custom ModelChoiceFields
See original GitHub issuei’m trying to build a custom autocomplete multiple model choice field. i started like this:
class SortedModelMultipleChoiceField(autocomplete_light.FieldBase, SortedMultipleChoiceField):
pass
But that results in a TypeError: __init__() takes at least 2 arguments (2 given) because ‘queryset’ is not in kwargs.
This is because parents in https://github.com/yourlabs/django-autocomplete-light/blob/master/autocomplete_light/fields.py#L33 doesn’t return forms.ModelMultipleChoiceField in my case; it only returns (<class 'autocomplete_light.fields.FieldBase'>, <class 'sortedm2m.forms.SortedMultipleChoiceField'>) even though sortedm2m.forms.SortedMultipleChoiceField is a subclass of forms.ModelMultipleChoiceField
Using __mro__ instead of __bases__ would solve the check.
For the time being, i’m just declaring the field this way:
class SortedModelMultipleChoiceField(autocomplete_light.FieldBase, SortedMultipleChoiceField, forms.ModelMultipleChoiceField):
"""
forms.ModelMultipleChoiceField is required as a parent class, even though
it's a parent class of SortedMultipleChoiceField
"""
pass
Issue Analytics
- State:
- Created 9 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Custom label on ModelChoiceField form field - django
To provide customized representations, subclass ModelChoiceField and override label_from_instance. So, in your case you could do:
Read more >Form fields - Django documentation
Each field has custom validation logic, along with a few other hooks. ... To provide customized representations, subclass ModelChoiceField and override ...
Read more >Change log for django-autocomplete-light - Read the Docs
#401: Easy to subclass FieldBase for custom ModelChoiceField (@smcoll),; #397: Support defining Meta in a ModelForm's parent.
Read more >django-autocomplete-light Changelog - PyUp.io
- 401: Easy to subclass FieldBase for custom ModelChoiceField (smcoll), - 397: Support defining Meta in a ModelForm's parent. - 394: Add comment...
Read more >django-autocomplete-light/CHANGELOG at master · yourlabs ...
- PEP396: autocomplete_light.__version__. - #401: Easy to subclass FieldBase for custom ModelChoiceField (@smcoll),. - #397: Support ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Since v3 is completely different, is there a similar approach to use dal with sortedm2m?
For the record, with this fix in addition to #331 , making an autocomplete version of sortedm2m’s SortedMultipleChoiceField was super clean: