Giter Club home page Giter Club logo

Comments (10)

brianmay avatar brianmay commented on June 28, 2024

@artscoop What problems/errors did this cause? I didn't see any errors caused by this problem with my project, so just wondering what is different. I don't use make_ajax_form, not convinced that would cause the problem however.

from django-ajax-selects.

brianmay avatar brianmay commented on June 28, 2024

In Django 1.7 release notes, I see "It is now possible to pass a callable as value for the attribute limit_choices_to when defining a ForeignKey or ManyToManyField". I can't see this causing the problems described though.

from django-ajax-selects.

artscoop avatar artscoop commented on June 28, 2024

I've just has the problem again (and forgot I already filed an issue) I use make_ajax_form in the admin.
Django 1.7 admin interface introduced code using limit_choices_to on form fields. However AutoCompleteSelectFields don't have this attribute set. Hence the program failing.

from django-ajax-selects.

crucialfelix avatar crucialfelix commented on June 28, 2024

if you have a chance to submit a pull request then the fix can be published
fairly quickly

On Mon, Nov 10, 2014 at 3:20 PM, artscoop [email protected] wrote:

I've just has the problem again (and forgot I already filed an issue) I
use make_ajax_form in the admin.
Django 1.7 admin interface introduced code using limit_choices_to on form
fields. However AutoCompleteSelectFields don't have this attribute set.
Hence the program failing.


Reply to this email directly or view it on GitHub
#83 (comment)
.

from django-ajax-selects.

brianmay avatar brianmay commented on June 28, 2024

AutoCompleteSelectFields inherits from forms.fields.CharField. It doesn't look like CharField defines or uses a limit_choices_to field.

Hence, I am still a bit confused what this limit_choices_to attribute is or why it is needed.

The best I can come up with is that Django admin is doing something dodgy.

Would be interested to see a full stack trace of the error. The only code I can see doesn't appear to be applicable (BaseModelForm init function) because AutoCompleteSelectFields doesn't define a queryset attribute.

from django-ajax-selects.

crucialfelix avatar crucialfelix commented on June 28, 2024

I glanced at the docs today. limit_choices_to sounds like it is optional.
the default is to not limit the choices at all -- query.set.all()

I'm surprised that they would introduce a breaking change like that, but
its entirely possible.

if I had time I would set up unit testing with travis so we would see all
the versions with the errors.

the example app is pretty easy to start up using any django version for
quick manual testing.

On Mon, Nov 10, 2014 at 11:21 PM, Brian May [email protected]
wrote:

AutoCompleteSelectFields inherits from forms.fields.CharField. It doesn't
look like CharField defines or uses a limit_choices_to field.

Hence, I am still a bit confused what this limit_choices_to attribute is
or why it is needed.

The best I can come up with is that Django admin is doing something dodgy.

Would be interested to see a full stack trace of the error. The only code
I can see doesn't appear to be applicable (BaseModelForm init function)
because AutoCompleteSelectFields doesn't define a queryset attribute.


Reply to this email directly or view it on GitHub
#83 (comment)
.

from django-ajax-selects.

brianmay avatar brianmay commented on June 28, 2024

I am starting to suspect it might actually be a Django bug. i.e. they broke something without intending to. As such it may deserve a Django bug report at https://code.djangoproject.com/query.

from django-ajax-selects.

artscoop avatar artscoop commented on June 28, 2024

This is the code breaking with AutoCompleteSelectField (django/forms/models.py, 333, in BaseModelForm.__init__):

    # Apply ``limit_choices_to`` to each field.
    for field_name in self.fields:
        formfield = self.fields[field_name]
        if hasattr(formfield, 'queryset'):
            limit_choices_to = formfield.limit_choices_to
            if limit_choices_to is not None:
                if callable(limit_choices_to):
                    limit_choices_to = limit_choices_to()
                formfield.queryset = formfield.queryset.complex_filter(limit_choices_to)

I've found it breaks only with one of my LookupChannels:

class PictureLookup(LookupChannel):
    model = Picture
    plugin_options = {'minLength': 2, 'delay': 500}

    def get_query(self, q, request):
        fields = ['title', 'description', 'image']
        final_query = search_query(q, fields)
        return Picture.objects.filter(final_query).order_by('-id')

    def get_result(self, obj):
        return obj.__unicode__()

    def format_match(self, obj):
        output = u"{thumb} {name}".format(thumb=obj.get_thumbnail_html(size=(48,20), template='picture'), name=obj.title or obj.description)
        return output

    def format_item_display(self, obj):
        output = u"<div>{thumb} {name}</div>".format(thumb=obj.get_thumbnail_html(size=(48,20)), name=obj.title or obj.description)
        return output

but it works like a charm with this LookupChannel:

class CityPublicMinimalLookup(CityLookup):
        model = City
        min_length = 4
        plugin_options = {'minLength': 4, 'delay': 0, 'position': {"my": "left bottom", "at": "left top", "collision": "flip"}}

    def get_query(self, q, request):
        fields = ['alternates__ascii']
        final_query = search_query(unidecode(q).strip(), fields)
        final_query = models.Q(city=True, country__public=True) & final_query
        items = City.objects.filter(final_query).distinct().order_by('-population')[0:10]
        return items

    def format_item_display(self,obj):
        output = u"""{country} <span class="text-muted">{code}</span> {name}""".format(name=obj.get_name(), country=obj.get_country_icon(directory="png"), code=obj.get_code())
        return output

from django-ajax-selects.

artscoop avatar artscoop commented on June 28, 2024

Wow. Finally nailed it. In my admin definition I added a queryset to a ForeignKey

def get_form(self, request, obj=None, **kwargs):
    form = super(ProfileAdmin, self).get_form(request, obj, **kwargs)
    if obj is not None:
        form.base_fields['picture'].queryset = obj.pictures.all()
    return form

I can't tell why it's mandatory for a FormField with a queryset to also have a limit_choices_to attribute. I'm going to file a Django ticket to know if that's a bug.

from django-ajax-selects.

crucialfelix avatar crucialfelix commented on June 28, 2024

yeah, that looks like a minor django bug. probably unintentional. it gets
quite hairy in the forms and admins and everybody is working with this
limited api.

for instance you cannot easily get the request and user when you are
filtering that query set. I had to make a custom admin that allows the
field to filter_by_request. for instance only show checkboxes for the
user's own items.

On Tue, Nov 11, 2014 at 3:52 AM, artscoop [email protected] wrote:

Wow. Finally nailed it. In my admin definition I added a queryset to a
ForeignKey

def get_form(self, request, obj=None, *_kwargs):
form = super(ProfileAdmin, self).get_form(request, obj, *_kwargs)
if obj is not None:
form.base_fields['picture'].queryset = obj.pictures.all()
return form

I can't tell why it's mandatory for a FormField with a queryset to also
have a limit_choices_to attribute. I'm going to file a Django ticket to
know if that's a bug.


Reply to this email directly or view it on GitHub
#83 (comment)
.

from django-ajax-selects.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.