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

@ -47,7 +47,7 @@ class Tag(TranslatedFieldsMixin, models.Model):
old_id = models.PositiveIntegerField(_('old id'), blank=True, null=True, default=None) old_id = models.PositiveIntegerField(_('old id'), blank=True, null=True, default=None)
old_id_meta_product = models.PositiveIntegerField(_('old id metadata product'), old_id_meta_product = models.PositiveIntegerField(_('old id metadata product'),
blank=True, null=True, default=None) blank=True, null=True, default=None)
objects = TagQuerySet.as_manager() objects = TagQuerySet.as_manager()

View File

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