question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Difficult to subclass FieldBase for custom ModelChoiceFields

See original GitHub issue

i’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:closed
  • Created 9 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
carlos22commented, Aug 3, 2018

Since v3 is completely different, is there a similar approach to use dal with sortedm2m?

0reactions
smcollcommented, Apr 28, 2015

For the record, with this fix in addition to #331 , making an autocomplete version of sortedm2m’s SortedMultipleChoiceField was super clean:

import autocomplete_light
from sortedm2m.forms import SortedMultipleChoiceField


class SortedModelMultipleChoiceField(autocomplete_light.FieldBase, SortedMultipleChoiceField):
    """
    Autocomplete ModelMultipleChoiceField with SortedM2M support
    """
    widget = autocomplete_light.MultipleChoiceWidget
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found