Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Auto complete for GenericForeignKey #5
I have the pleasure to announce that a generic fk autocomplete was implemented in a sane way with only 80 Python slocs.
The patches in the django ticket didn't convince me, so I did not use them. Actually, the patch attempts to fix GFK support at the admin level rather than the form level.
The thing with GFK is that they are "virtual fields". Django database stuff doesn't take care of "virtual fields", and that's normal since a virtual field doesn't have a database representation.
Django ModelForm doesn't take care of "virtual fields" neither. And that's pretty confusing, which shouldn't a virtual field like a GenericForeignKey not be able to be represented in the form ? Is it a bug or design decision ?
Hopefully, fixing Django's ModelForm support for virtual field was trivial: GenericModelForm. So, kudos to Django maintainers for not merging these patches, a very wise decision !
Once the modelform supports virtualfields, it becomes possible to make a form field for a virtual field. I made a trivial field that converts a string value of '%s-%s' % (content_type_id, object_id) into a model, and vice versa.
Another thing from Django required to be fixed: reverse 'object_id' and 'content_type' from 'content_object' in a pre-save signal. Again, this is trivial, I wonder why Django doesn't do that out of the box.
Finally, GenericChannelBase is a trivial implementation of ChannelBase that just wraps around several querysets rather than one. I like it a lot because it also demonstrates how to completely abstract the model-related stuff from ChannelBase (as that logic that depends on a model and queryset has been totally replaced). Check out the example usage.
The only problem I've had (apart from fixing django's modelform and GFK pre-save), is that the current implementation of ChannelRegistry registers a channel with a model. Which means that I had to low-level register the generic channel which works but well is "not supported".
Now, we can expect this to get into autocomplete-light 0.4, what needs to be done until then is just:
Of course, the next objective would be to get M2M gfks. That sounds like a lot of fun, plus some interesting research has been done
Again, fantastic idea, thank a lot for your feedback.
In the 0.4 release. Documentation: http://django-autocomplete-light.readthedocs.org/en/latest/generic.html
Does this match your expectations ?
Today: generic m2m ;)
You're welcome ! We needed that too actually we had an horrible "Generic Relations" stacked inline ... I could remove a nice bunch of code from our project and replace it with this awesome widgets so I'm very happy yeahhhhh
FWY, I just released 0.5 which officially supports generic m2m. See the demo/test procedure: https://github.com/yourlabs/django-autocomplete-light/blob/master/test_project/INSTALL.rst It literally takes 1 minute, so don't be afraid and try it hehe
Well, it works in CI, could you isolate your issue in a small test project or app ?