如何从序列化器字段中订购django rest framework queryset?

时间:2017-07-26 03:43:29

标签: django django-rest-framework django-queryset

如何从序列化器字段中订购Django QuerySet?由于该字段太复杂,我无法使用QuerySetannotate进行排序,我也无法将该值存储在模型中。

编辑:

串行

class DrinkListModelSerializer(serializers.ModelSerializer):
    count_need = serializers.SerializerMethodField()
    url = drink_detail_url

    class Meta:
        model = Drink
        fields = [
            'name',
            'count_need',
            'thumbnail',
            'url'
        ]

    def get_count_need(self, obj):
        drink_ingredient_qs = obj.ingredients.all()
        user = self.context['request'].user
        user_qs = User.objects.filter(username=user.username)
        if user_qs.exists() and user_qs.count() == 1:
            user_obj = user_qs.first()
            user_ingredient_qs = user_obj.ingredient_set.all()
            return (user_ingredient_qs & drink_ingredient_qs).count()
        return 0

型号:

class Drink(models.Model):
    name                = models.CharField(max_length = 1000, null=True, blank=True)
    ingredients         = models.ManyToManyField(Ingredient, blank=True)
    user                = models.ManyToManyField(settings.AUTH_USER_MODEL, blank=True)

class Ingredient(models.Model):
    name                = models.CharField(max_length = 1000)
    ingredient_category = models.ForeignKey(Category, null=True, blank=True)
    slug                = models.SlugField(max_length=255, unique=True)
    user                = models.ManyToManyField(settings.AUTH_USER_MODEL, blank=True)

查看

class DrinkListAPIView(ListAPIView):
serializer_class = DrinkListModelSerializer
pagination_class = LargeResultsSetPagination
permission_classes = [AllowAny]

def get_queryset(self, *args, **kwargs):
    user = self.request.user
    qs = Drink.objects.all()
    userQuery = self.request.GET.get('user')
    query = self.request.GET.get("q")
    filters = self.request.GET.getlist('filter')
    if query:
        qs = qs.filter(name__icontains=query)
    elif filters:
        qs = qs.filter(playlist__name__iexact=filters[0])
        for filter in filters[1:]:
            qs = qs | Drink.objects.filter(playlist__name__iexact=filter)
    elif userQuery and user.is_authenticated():
        qs = qs.filter(user=user)
    return qs.order_by('-timestamp')

目前,我按时间戳排序查询集,因为它在模型中是自己的,但我需要通过' count_need'来订购它。这是序列化器的一部分。我试图做qs = qs.annotate(count_need=Count('ingredients' & user_ingredient_qs)).order_by(count_need)这样的事情,但这显然不起作用,因为'成分'这里只是一个字符串,而不是Drink的成分查询集。

1 个答案:

答案 0 :(得分:1)

可能运行良好的一种方法是在列表序列化器上实现和自定义""

to_representation