Hi, 

I have an issue with regard to exposing a RESTful interface using 
filtersets and HyperLinkedModelserialisers. I have two model classes 
defined thus:

class Manufacturer(CommonExtendedIcon):
    WebSite=models.CharField(max_length=1024)
    StandardList=models.BooleanField(default=False)
    SourceURL=models.CharField(max_length=1024,null=True)
    LastChecked=models.DateTimeField(null=True)
    def __str__(self):
        return self.Name

class ManufacturerModel(CommonExtendedIcon):
    Manufacturer=models.ForeignKey(Manufacturer, related_name='models')
    WebSite=models.CharField(max_length=1024)
    SeriesStartDate=models.IntegerField(null=True)
    SeriesEndDate=models.IntegerField(null=True)
    SourceUrl=models.CharField(max_length=1024,null=True, default='')
    LastChecked=models.DateTimeField(null=True)
    def __str__(self):
        return self.Name

Now in my my rest Interface I make a query to retrieve say all of the 
Manufacturer Models with a Manufacturer begining with 'A'. Looking at the 
SQL that is subsequently run I can thus see that there are over 4000 
queries issued. 1 to retrieve all of the ManufacturerModel Records and then 
1 for EACH individual linked Manufacturer. 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.  

Here are my serialiser classes:-

class ManufacturerSerialiser(serializers.HyperlinkedModelSerializer):
    class Meta:
            model = Manufacturer
            fields = 
('WebSite','LogoLocation','Name','id','StandardList','SourceURL')
            
class ManufacturerModelSerialiser(serializers.HyperlinkedModelSerializer):
    manufacturer = 
serializers.HyperlinkedIdentityField(view_name='manufacturer')
    class Meta:
        model = ManufacturerModel
        fields = 
('WebSite','LogoLocation','Name','id','Manufacturer','SeriesStartDate','SeriesEndDate','SourceUrl','LastChecked')

And Finally my FilterSets

class ManufacturerFilter(django_filters.FilterSet):
        Name = django_filters.CharFilter(lookup_type='icontains')
        Website = django_filters.CharFilter(lookup_type='icontains')
        class Meta:
            model=Manufacturer
            fields = ('WebSite','LogoLocation','Name','id','StandardList')
            
            
class ManufacturerModelFilter(django_filters.FilterSet):    
        Manufacturer=django_filters.CharFilter(name='Manufacturer__Name', 
lookup_type='icontains')
        Name = django_filters.CharFilter(lookup_type='icontains')
        Website = django_filters.CharFilter(lookup_type='icontains')
        class Meta:
            model=ManufacturerModel
            fields = 
('WebSite','LogoLocation','Name','id','Manufacturer','LastChecked')


I know that If I remove the 'Manufacturer' field  from the 
ManufacturerModel serialisation classes it works as expected meaning that 
it is the relationship that is causing this behaviour (the extra 4000 SQL 
Queries) .

-- 
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/12b75d3a-0610-4d66-bcb8-e265cd0c2505%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to