Skip to content
This repository

Auto complete for GenericForeignKey #5

Closed
user996015 opened this Issue May 19, 2012 · 8 comments

3 participants

user996015 James Pic swiharta
user996015

Any chance this could be adapted to work with generic foreign keys?

Some work has been done here.
https://code.djangoproject.com/ticket/9976

James Pic
Owner
jpic commented May 20, 2012

That's a very good idea.

I'll try to push a POC later today.

Thank you for your feedback.

James Pic
Owner
jpic commented May 20, 2012

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:

  • consolidate GenericModelForm which has high expectations and thus may not work in all cases
  • allow registration of channels without models
  • document all that
  • release 0.4

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.

James Pic
Owner
jpic commented May 20, 2012

In the 0.4 release. Documentation: http://django-autocomplete-light.readthedocs.org/en/latest/generic.html

Does this match your expectations ?

Today: generic m2m ;)

James Pic
Owner
jpic commented May 21, 2012

Got generic m2m autocomplete support ... well, that was fast !

James Pic jpic closed this May 21, 2012
user996015

Excellent. Thank you

James Pic
Owner
jpic commented May 22, 2012

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

Rock'on B)

swiharta

This is awesome man. I randomly found out about this app when searching for an alternative to django-ajax-selects. I think the word needs to get out about the great work you've been doing on it!

James Pic
Owner
jpic commented May 23, 2012

Thanks for your feedback ! Please let me know what issues you run into, also if they are documentation-related, you can use the mailing list.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.