AssertionError: Cannot filter a query once a slice has been taken.
Personally I don't feel that it's a particularly common need, but I can see your use case. More common is that a reasonable filter is applicable - say books with a rating over 80.
As far as I know we don't have any support for SQL intersects and I don't believe there are any plans to introduce it.
I do agree that this should be both documented and preferably raise an error - please open a ticket.
As for a work around, using a ChoiceField directly shouldn't be particularly difficult in this case. ModelChoiceField has some nice features but it is limited.
Marc
--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/e264b254-4208-4e39-a50b-6657f8817996%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Personally I don't feel that it's a particularly common need, but I can see your use case. More common is that a reasonable filter is applicable - say books with a rating over 80.
As far as I know we don't have any support for SQL intersects and I don't believe there are any plans to introduce it.
I do agree that this should be both documented and preferably raise an error - please open a ticket.
The obvious nicety handled by
ModelChoiceField is the transformation from ids back to objects; the less
obvious one is that a ModelChoiceField runs the query anew for every form;
your ChoiceField cannot be initialized by a query in its definition, and needs
to have its choices set in the form's __init__().
you can base your condition on some subquery, for instance :class BookForm(forms.Form):
book = forms.ModelChoiceField(queryset=Book.objects.filter(pk__in=Book.objects.order_by('-rating')[:100].values_list('pk')))
I don't know if there is a better way to do that...
--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/044f428c-8192-48ff-8619-21b22833a7fd%40googlegroups.com.
However, I don't think this is subject to the kind of problem you describe as the inner queryset should be turned into a subquery of the main queryset.
It's true that the generated queries might be inefficient (I'm not sure all DBMS can optimize something like "SELECT ... FROM foo WHERE id IN (SELECT id FROM foo ORDER BY bar LIMIT 10)"). I would not use such a construction on a big production dataset and if performance really matters I would do something else.
EXPLAIN ANALYZE SELECT
"baseapp_award"."id",
"baseapp_award"."profile_awards_id",
"baseapp_award"."name",
"baseapp_award"."date_achieved",
"baseapp_award"."recognition_level"
FROM "baseapp_award"
ORDER BY "baseapp_award"."id" DESC
LIMIT 100;
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=0.28..4.51 rows=100 width=38) (actual time=0.024..0.118 rows=100 loops=1)
-> Index Scan Backward using baseapp_award_pkey on baseapp_award (cost=0.28..80.94 rows=1906 width=38) (actual time=0.022..0.097 rows=100 loops=1)
Total runtime: 0.171 ms
EXPLAIN ANALYZE SELECT
"baseapp_award"."id",
"baseapp_award"."profile_awards_id",
"baseapp_award"."name",
"baseapp_award"."date_achieved",
"baseapp_award"."recognition_level"
FROM "baseapp_award"
WHERE
"baseapp_award"."id" IN (
SELECT U0."id"
FROM "baseapp_award" U0
ORDER BY U0."id" DESC
LIMIT 100
);
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Hash Semi Join (cost=6.76..48.94 rows=100 width=38) (actual time=0.763..0.841 rows=100 loops=1)
Hash Cond: (baseapp_award.id = u0.id)
-> Seq Scan on baseapp_award (cost=0.00..36.06 rows=1906 width=38) (actual time=0.007..0.324 rows=1908 loops=1)
-> Hash (cost=5.51..5.51 rows=100 width=4) (actual time=0.112..0.112 rows=100 loops=1)
Buckets: 1024 Batches: 1 Memory Usage: 4kB
-> Limit (cost=0.28..4.51 rows=100 width=4) (actual time=0.009..0.083 rows=100 loops=1)
-> Index Only Scan Backward using baseapp_award_pkey on baseapp_award u0 (cost=0.28..80.94 rows=1906 width=4) (actual time=0.009..0.063 rows=100 loops=1)
Heap Fetches: 100
Total runtime: 0.904 ms