셀러 페이지 구현

1432 단어 djangodjango

코드

class SellerView(View):
    def get(self, request):
        category = request.GET.get("category", "")

        user_q    = Q()
        product_q = Q()

        if category in Origin.Type.names:
            user_q    &= Q(product__origin_id=Origin.Type.names.index(category)+1) 
            product_q &= Q(origin_id=Origin.Type.names.index(category)+1)
        elif category in Storage.Type.names:
            user_q    &= Q(product__storage_id=Storage.Type.names.index(category)+1)
            product_q &= Q(storage_id=Storage.Type.names.index(category)+1)

        users = User.objects.filter(user_q).prefetch_related(
                Prefetch('product_set', queryset=Product.objects.filter(product_q), to_attr='category'),
        ).order_by('-id').distinct()

        sellers = []
        for user in users:

            category = list(set([Origin.Type(product.origin_id).name for product in user.category]
                            + [Storage.Type(product.storage_id).name for product in user.category]))

            sellers.append({
                "id"            : user.id,
                "kakao_account" : user.kakao_account,
                "name"          : user.name,
                "profile_image" : user.profile_image,
                "category"      : category
            })

        return JsonResponse({"seller": sellers}, status=200)

좋은 웹페이지 즐겨찾기