Hi Javier, 

Thank you for your quick response. Sadly I could not get your solution to 
work however you helped to point me in the right direction to a workable 
though not entirely satisfactorey solution. What I did was to refactor my 
ViewSet Class thus:-

class ManufacturerModelViewSet(viewsets.ModelViewSet):
    model=ManufacturerModel
    serializer_class=ManufacturerModelSerialiser
    filter_class=ManufacturerModelFilter
    def get_queryset(self):
        queryset = ManufacturerModel.objects.all().select_related()
        return queryset

Now this works, to a fashion! Instead of 4000 queries I now get 2 queries 
(the latter query bringing in ALL of the manufacturers even without the 
filter that I specified on the Manufacturer table, Bygone...). This is 
still way more peformant and at least usable if not quite right! 

I would still be keen to know if there is any know method that would allow 
me to load this data using only the 1 queries bringing in all of the 
related fields in the original query.  

Thanks again for your help.

Kind regards

Conrad

On Thursday, 10 April 2014 14:58:36 UTC+1, Javier Guerra wrote:
>
> On Thu, Apr 10, 2014 at 8:29 AM, Conrad Rowlands 
> <[email protected] <javascript:>> wrote: 
> > Why is this and how can I stop this behaviour but still bring back the 
> > Manufacturer Details. I expected instead the first query to load all of 
> the 
> > data. 
>
>
> override the get_collection() method in your View (or ViewSet) to add 
> a select_related(). 
>
> for example, something like this (untested!): add this mixin to your 
> View and optionally set the related_fields: 
>
> class SelectRelatedMixin(object): 
>     related_fields = () 
>     def get_queryset(self): 
>         return super(SelectRelatedMixin, 
> self).get_queryset().select_related(*self.related_fields) 
>
>
> class ManufacturerModelViewSet(SelectRelatedMixin, viewsets.ModelViewSet): 
>     related_fields = ('manufacturer',) 
>     ..... the rest of your viewset 
>
> -- 
> Javier 
>

-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/c049cdb2-019a-4974-aaec-ccba168917f2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to