yourlabs / django-autocomplete-light

A fresh approach to autocomplete implementations, specially for Django. Status: v4 alpha, v3 stable, v2 & v1 deprecated.
https://django-autocomplete-light.readthedocs.io
MIT License
1.8k stars 467 forks source link

Forward id for a models.ForeignKey("self") #1346

Open lsmith77 opened 9 months ago

lsmith77 commented 9 months ago

I want to prevent being abel to select the model itself.

But forward.Field("id", "ignore_id"), doesn't work since there is no "id" form field. What is the easiest way to handle this?

I guess I could write a javascript handler to look at the URL, but maybe there is a simpler way to achieve this.

jpic commented 9 months ago

Can you add the id in a GET param of the autocomplete url and then filter it out?

lsmith77 commented 8 months ago

Here is the "solution" I found by inspecting the generated HTML but it feels very hacky:

                forward=(
                    # Hacky workaround for https://github.com/yourlabs/django-autocomplete-light/issues/1346
                    forward.Field("children-__prefix__-parent", "ignore_id"),
                    forward.Field("language"),
                ),

It seems like children-__prefix__-parent contains the ID I need.

I have not figured out how I can dynamically add a GET parameter.