Tuesday, August 30, 2011

Re: Combining queries? A "join" in Python?

I think I need to restate this question with the actual models. Sorry
again. I have omitted irrelevant fields (ones I do not want too query
against) replacing them with dots.

class Category(models.Model):
order = models.IntegerField(default=100)
..........

class CategoryText(models.Model):
language = models.CharField(max_length=2,choices=LANG_CHOICES)
category = models.ForeignKey(Category,related_name='texts')
name = models.CharField(max_length=500)
............
class Meta:
unique_together = ('language','category')

class SubCategory(models.Model):
category = models.ForeignKey(Category)
order = models.IntegerField(default=100)
............

class SubCategoryText(models.Model):
language = models.CharField(max_length=2,choices=LANG_CHOICES)
subcategory = models.ForeignKey(SubCategory,related_name="texts")
..................
class Meta:
unique_together = ('language','subcategory')

CategoryText and SubCategoryText contain translations. ON any given
page I want those for one language.

My current queryset is:

SubCategoryText.objects.filter(language=lang).annotate(subcat_places=(Count('subcategory__places'))).exclude(subcat_places=0).select_related().order_by('subcategory__category__order','-
subcat_places','name')

if I iterate over this in the template it gives me almost what I want,
but I want to be able to do something like this.

{{listing.subcategory.category.texts.name}}

and get back the name from the CategoryText for the current language.

I have tried filtering in various ways.

I am currently trying looping over the queryset and adding attributes
to each model with the name from the CategoryText and the category url
(which are what I need in the template) , but this feels a bit hackish
(assuming it works).

On Aug 30, 12:02 pm, graeme <graeme.piete...@gmail.com> wrote:
> On Aug 30, 6:25 am, Tim <timothyp...@gmail.com> wrote:
>
> > worst case you could always just write the sql query?
>
> >https://docs.djangoproject.com/en/dev/topics/db/sql/
>
> I have been looking at that already, what I have not yet figured out
> is how the results map to models if I do a join in the SQL query. Can
> it create the models for the joined tables as well?
>
>
>
>
>
>
>
> > On Aug 29, 8:15 am,graeme<graeme.piete...@gmail.com> wrote:
>
> > > I looks like my attempt to simplify and abstract the problem just made
> > > it harder to help me: my apologies for that. I was trying to combined
> > > two different problems rather than ask two questions. Thanks for
> > > helping despite that.
>
> > > I think I have a solution for most of my problems, as far as getting
> > > the queries working. I might be back with questions about
> > > optimisation, but I can live with a little inefficiency in the query I
> > > need to get working first.
>
> > > On Aug 28, 4:33 am, Matteius <matte...@gmail.com> wrote:
>
> > > > Since you are combining two sets of different objects you'll want
> > > > Gelonida's response.  Additionally, Use Q to create more logically
> > > > advanced queries.  I find your language difficult to gather what query
> > > > you are looking for exactly, but here is an example of what I think
> > > > you might mean:
>
> > > > from django.db.models import Q
>
> > > > the_Ds = D.objects.all().filter(b=B)
> > > > the_Es = E.objects.all().filter(c=C)
>
> > > > combined = the_Ds | the_Es
>
> > > > # Other Example (an & requires both constraints to be met, and an Or
> > > > will include the set of all matches.
> > > > the_As = A.objects.all().filter(Q(constraint1) & Q(constraint2))
> > > > the_Bs = B.objects.all().filter(Q(constraint1) | Q(constraint2))
>
> > > > -Matteius
>
> > > > Don't overlook how powerful this is because Django has Manager classes
> > > > so you can make your constraints refer to both local properties and
> > > > foreign key constraints as well as properties within the foreign key
> > > > lookups.  You may find that you wish to edit your schema relationship,
> > > > perhaps by pointing backwards or reversing the relationship.  It is
> > > > hard to say with the categorical A, B, C, D example, but hope this
> > > > helps and Good Luck!
>
> > > > On Aug 27, 3:47 pm, Gelonida N <gelon...@gmail.com> wrote:
>
> > > > > On 08/27/2011 11:39 AM,graemewrote:
>
> > > > > > I have a some models related link this:
>
> > > > > > A has a foreign key on B which has a foreign key on C
>
> > > > > > I also have D with a foreign key on B, and E with a foreign key of C
>
> > > > > > If I do A.filter(**args).select_related() I will get all the As, Bs,
> > > > > > and Cs I want.
>
> > > > > > How do I get the Ds with a foreign key on a B in my queryset
> > > > > > (preferably filtered) and all Es with a foreign key on C (also
> > > > > > preferably filtered)
>
> > > > > > The best I have been able to come up with is to query for all the  the
> > > > > > Ds and Es I want, and combine them with the Bs and Cs in the view.
>
> > > > > > I have a similar problem with another site, except that there not
> > > > > > every B I want has an A with a foreignkey pointing to it, so I cannot
> > > > > > just do select_related on A.
>
> > > > > > I am not worried about doing an extra query or two per page. What I
> > > > > > want to avoid is doing an extra query for each object in a lengthy
> > > > > > list.
>
> > > > > You can 'or' two query sets with the '|' operator
>
> > > > > so do
> > > > > queryset1 = Mymodel.objects.all().filter(...)
> > > > > qyeryset2 = Mymodel.objects.all().filter(...)
>
> > > > > combined = queryset1 | queryset2

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment