tags fix mtm

This commit is contained in:
alex 2019-12-04 09:23:21 +03:00
parent 7596652a3f
commit 7590a73c5d
2 changed files with 12 additions and 11 deletions

View File

@ -2,11 +2,9 @@
from rest_framework import serializers
from rest_framework.fields import SerializerMethodField
from establishment.models import (Establishment, EstablishmentType,
EstablishmentSubType)
from establishment.models import (Establishment, EstablishmentType)
from news.models import News, NewsType
from tag import models
from tag.models import Tag
from utils.exceptions import (ObjectAlreadyAdded, BindingObjectNotFound,
RemovedBindingObjectNotFound)
from utils.serializers import TranslatedField
@ -79,18 +77,21 @@ class TagCategoryBaseSerializer(serializers.ModelSerializer):
def get_tags(self, obj):
query_params = dict(self.context['request'].query_params)
if len(query_params) > 1:
return None
tags = Tag.objects.all()
return []
params = {}
if 'establishment_type' in query_params:
types = query_params['establishment_type']
tags = tags.filter(establishments__establishment_type__index_name__in=types).distinct()
params = {
'establishments__isnull': False,
}
elif 'product_type' in query_params:
types = query_params['product_type']
tags = tags.filter(products__product_type__index_name__in=types).distinct()
params = {
'products__isnull': False,
}
tags = obj.tags.filter(**params).distinct()
return TagBaseSerializer(instance=tags, many=True, read_only=True).data