On 17 heinä, 00:53, Tomáš Ehrlich <[email protected]> wrote:
> Hi there,
> I'm trying to write (another) app for model translation. It's
> necessary to somehow filter language=get_language (or whatever)
> implicitly. The thing is, I need to specify this filter for every
> translated table in join.
You are going to face a big problem for LEFT JOINs. You must add the
filter condition directly to the join clause to get correct results,
but Django has zero support for that currently.
> First idea which crossed my mind is subclass
> django.db.models.sql.query.Query and add desired filters in
> get_compiler, then subclass django.db.models.query.Queryset's __init__
> (to use new Query class) and finally create custom Manager.
>
> Questions are:
> 1. What if non-translated model had relation to translated one. Would
> it consider customized queryset?
> 2. Is there better way to create this 'implicit where clause' ?
Unfortunately I don't believe you can make the above work, at least
with any sane amount of work.
If you want to push this route, then I would advice you to create a
custom compiler which adds the filter conditions directly to the join
clause (regardless if it is LEFT or INNER join). You will need to
override the part of the code where the join clauses are created. I am
not saying this will be easy, but the chances to get this to work
reliably are better than adding the conditions to the where clause.
As for future in-core development ideas, I have been long thinking
that a way to do model annotations would be an excellent feature.
Something like this:
Article.objects.annotate(trans_fi=ModelAnnotation('translations',
Q(translations__lang='fi'))).filter(trans_fi__title__icontains='django').order_by('trans_fi__title')
- Anssi
--
You received this message because you are subscribed to the Google Groups
"Django developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-developers?hl=en.