-
Notifications
You must be signed in to change notification settings - Fork 467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement DRY to all autocompletes #1067
Comments
Hello @jpic, actually I've found a nice solution for this: Using the existing django modelform_factory and passing it the
I know that this doesn't actually save many keystrokes (you just define the form using the modelform_factory instead of using a separate form class) but it definitely is more DRY than having an extra I recommend putting this as a hint to the users to the tutorial in the "Using autocompletes in the admin" section (https://django-autocomplete-light.readthedocs.io/en/master/tutorial.html#using-autocompletes-in-the-admin), i.e something like
|
But you will still need to create a view and register an url for the widget ? |
@Guilouf yes I'd need to create both the view and widget. What I mainly wanted was to avoid creating an explicit form class for this and having something similar to the django-autocomplete-light v2 modelform_factory (https://django-autocomplete-light.readthedocs.io/en/2.0.9/tutorial.html#autocomplete-light-modelform-factory-shortcut-to-generate-modelforms-in-the-admin). Also probably the v2 modelform_factory behavior could be better emulated if v3 provided an |
@spapas Yeah you will need to create a custom So then you will have the choice between: class TForm(autocomplete.FutureModelForm):
"""
django widgets converted to dal widgets automatically, but still the possibility to customize
as in standard django's forms
"""
class Meta:
model = TModel
fields = __all__ or |
You're both right that we can have it like in v2 from the outside but also not like v2 in the inside. Creating the fields on the fly is possible safely in the metaclass, but unlike in v2 it should all be contained in FutureFormMetaclass.new before calling super().new to emulate that the user has defined them normally - in the way that is currently tested. However with @Guilouf's pattern I suppose you will have to solve a chicken and egg problem if the form class is also supposed to register urls, that means after being defined by the metaclass. This means that the widgets will have to initialize without a url, and be set afterwards when the form generates the urls... Sounds like a lovely hack 😹 |
if you want DRY, you need to make either the view generate the form field, either the form field generate the view, like what @Guilouf does
@Guilouf found a pattern , it's currently in dal_queryset_sequence, but can be extended and used in others.
then he does urlpatterns += form.as_urls() or something
https://github.com/yourlabs/django-autocomplete-light/blob/master/src/dal_select2_queryset_sequence/fields.py#L44
The text was updated successfully, but these errors were encountered: