Hi,

I am trying to optimise Django queries on my ecommerce website. One of the 
fundamental query is where I have no clue how to make efficient. It could 
be trivial and probably been known long time back. But I am new to Django 
and would appreciate any help. This is primarily for one to many or many to 
many relations.

Following is an example scenario:
(Please pardon my syntax as I want to put across the concept and not the 
exact django code unless it's really needed):

Model A:

class A(models.Model):
    # Fields of model A

Model B (which is related to A with foreign key):

class B(models.Model):
    a = models.ForeignKey('A', related_name='bs')

Now I would like to find out all As for which there is atleast one b. The 
only way I know is as follows:

A.objects.filter(bs__isnull=False)

But the above isn't an optimal way as with large of records in A and B, the 
above takes lot of time. It gets more inefficient if it's a many to many 
relationship.

Could anyone please let me know the most efficient way to use django 
queryset for the above scenario?

Thanks.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/4817c6c7-987b-4b71-aef3-3ed910e1be5e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to