tags:

views:

36

answers:

0

Hi,

I have a problem with ModelChoiceField, it re-runs the query set every time, I am trying to cache my querysets here and I noticed that ModelChoiceIterator uses queryset.all() when iterating, which creates a new queryset, without cached data, and triggers a fresh query on the database.

Is there any way past that?

I am using cached querysets for a lot of things but my ModelChoiceFields use dynamic querysets, set at construction (in the init) because I need some parameters for the query:

class NewProductForm(forms.Form):
    group = forms.ModelChoiceField(
        queryset = ProductGroup.objects.none(),
        empty_label = None,
        cache_choices = True)

    def __init__(self, request_spec, *args, **kwargs):
        super(NewProductForm, self).__init__(*args, **kwargs)

        product_groups = ProductGroup.objects.filter(catalogue__decisionboard__req = request_spec).distinct()
        product_groups = get_cached_queryset(product_groups)

        self.fields['group'].queryset = product_groups

get_cached_queryset caches or retrieves from the cache the queryset, whichever way, the queryset is re-executed when the form is rendered, because of what I say above.

Any simple way? (without having to override ModelChoiceField)

Thanks