Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am attempting to fetch nested objects but not quite sure how to achieve this. My model is as shown:

class Brand(models.Model)
    name = models.CharField(max_length=128)

class Size(models.Model)
    name = models.CharField(max_length=128)

class Brand_Size(models.Model)
    brand = models.ForeignKey(Brand)
    size = models.ForeignKey(Size)

class Brand_Size_Location(models.Model)
    location = models.ForeignKey(Location)
    brand_size = models.ForeignKey(Brand_Size)

I filter objects in Brand_Size_Location by location which can occur 1..x. I want my serializer to output the results in terms of the model Brand (BrandSerializer). Since my resultset can be 1..x and furthermore the occurrence of Brand can be duplicates i would like to eliminate these aswell at the same time.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

You should be able to do this fairly easily by including a serializer field in your BrandSerializer:

class BrandSerializer(serializers.ModelSerializer):
    brand_sizes = BrandSizeSerializer(
        source='brand_size_set',
        read_only=True
    )

    class Meta:
        model = Brand
        fields = (
            'id',
            ...add more fields you want here
            'brand_sizes')

You can simlarly create the brand size serializer to nest the locations

Filtering on this relationship occurs in the view and will need a custom filter. Here's a basic example using a ListView:

class BrandFilter(django_filters.FilterSet):
    location = django_filters.ModelMultipleChoiceFilter(
        queryset=Brand_Size_Location.objects.all(),
        name='brand_size__brand_size_location__location'
    )
    location_name = django_filters.CharFilter(
        name='brand_size__brand_size_location__location__name'
    )

class Meta:
    model = Brand
    fields = [
        'location',
        'location_name'
    ]

class BrandList(LoginRequiredMixin, generics.ListCreateAPIView): model = Brand serializer_class = BrandSerializer filter_class = BrandFilter

You can then use query parameters to filter on the URL like:

http://somehost/api/brands/?location=42

which uses a PK to filter, or with the location name (assuming you have a name field in the Location model):

http://somehost/api/brands/?location_name=Atlantis
share|improve this answer
    
So the result i get from filtering objects in Brand_Size_Location i simply serialize with BrandSerializer? Or how is this done in practice.. –  JavaCake Apr 19 '14 at 16:10
    
@JavaCake Not sure if I'm following completely, but filtering isn't done in the serializer, but in the view. To filter on a nested relationship will require writing a custom filter, which is quite easy actually...I'll update my answer with an example –  Fiver Apr 19 '14 at 16:48
    
Im sorry for misleading you with my question. Basically what i mean is, can i take the resultset from my filtering of Brand_Size_Location and apply the serializer to it (BrandSerializer)? –  JavaCake Apr 19 '14 at 16:51
1  
Ahh, so you are using Brand_Size_Location as the model for the view, and want to have a nested Brand field with it's own nested info? –  Fiver Apr 19 '14 at 17:04
    
Exactly. The filtering is occuring on the Brand_Size_Location model. Where i become a little confused is how to serialize it so deep. I attempted to mess around with depth but i did not exactly get what i wanted. So what i want is to take my resultset from the filtering, get all Brand_Size followed by Brand where i eliminate the duplicates. So basically i will end up with a list of Brand items. –  JavaCake Apr 19 '14 at 17:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.