Skip to content


Auto complete for GenericForeignKey #5

user996015 opened this Issue · 10 comments

4 participants


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

Some work has been done here.

YourLabs member

That's a very good idea.

I'll try to push a POC later today.

Thank you for your feedback.

YourLabs member

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.

YourLabs member

In the 0.4 release. Documentation:

Does this match your expectations ?

Today: generic m2m ;)

YourLabs member

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

@jpic jpic closed this

Excellent. Thank you

YourLabs member

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: It literally takes 1 minute, so don't be afraid and try it hehe

Rock'on B)


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!

YourLabs member

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.


I'm not sure why, but save_m2m doesn't save any multiple choice fields with generic relations for me. Does anyone else have such issue? I'm using django 1.7 and autocomplete-light 2.2.10

YourLabs member

Well, it works in CI, could you isolate your issue in a small test project or app ?

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.