默认情况:rdf是查询到所有信息

python django rdf 的高手们,你们要么不讲核心内容,要么浅尝辄止

我自己慢慢琢磨这些,耽误我好久时间。

这里设计技术【中间件(改造request新属性,便于传递信息)、跨表查询、条件查询、改造序列化器】

class OrderSerializer(serializers.ModelSerializer):
    class Meta:
        model = Order
        fields = "__all__"
class OrderViewSet(viewsets.ModelViewSet): queryset = Order.objects.all() serializer_class = OrderSerializer

 得到的结果是:

【包含了两个人的结果:"customer_name": "魏芳"+李雷 】

[
	{
		"customer_id": 1,
		"customer_name": "李雷",
		"product_name": "车蜡",
		"quantity": 1,
		"amount": 20,
		"buy_time": "2022-12-27 22:48:48"
	},
	{
		"customer_id": 1,
		"customer_name": "李雷",
		"product_name": "车蜡",
		"quantity": 1,
		"amount": 20,
		"buy_time": "2022-12-27 23:05:20"
	},
	{
		"customer_id": 1,
		"customer_name": "李雷",
		"product_name": "车蜡",
		"quantity": 1,
		"amount": 20,
		"buy_time": "2022-12-28 08:06:27"
	},
	{
		"customer_id": 1,
		"customer_name": "李雷",
		"product_name": "车蜡",
		"quantity": 1,
		"amount": 20,
		"buy_time": "2022-12-28 08:11:57"
	},
	{
		"customer_id": 1,
		"customer_name": "李雷",
		"product_name": "车蜡",
		"quantity": 1,
		"amount": 20,
		"buy_time": "2022-12-28 08:11:59"
	},
	{
		"customer_id": 1,
		"customer_name": "李雷",
		"product_name": "车蜡",
		"quantity": 2,
		"amount": 40,
		"buy_time": "2022-12-28 08:13:08"
	},
	{
		"customer_id": 1,
		"customer_name": "李雷",
		"product_name": "车载香水",
		"quantity": 1,
		"amount": 120,
		"buy_time": "2022-12-28 08:13:58"
	},
	{
		"customer_id": 1,
		"customer_name": "李雷",
		"product_name": "车贴",
		"quantity": 1,
		"amount": 20,
		"buy_time": "2022-12-28 18:16:08"
	},
	{
		"customer_id": 1,
		"customer_name": "李雷",
		"product_name": "车贴",
		"quantity": 1,
		"amount": 20,
		"buy_time": "2022-12-28 18:16:31"
	},
	{
		"customer_id": 2,
		"customer_name": "魏芳",
		"product_name": "车蜡",
		"quantity": 1,
		"amount": 20,
		"buy_time": "2022-12-29 12:21:29"
	},
	{
		"customer_id": 2,
		"customer_name": "魏芳",
		"product_name": "车载香水",
		"quantity": 1,
		"amount": 120,
		"buy_time": "2022-12-29 12:21:36"
	},
	{
		"customer_id": 2,
		"customer_name": "魏芳",
		"product_name": "机油",
		"quantity": 1,
		"amount": 300,
		"buy_time": "2022-12-29 12:21:41"
	},
	{
		"customer_id": 2,
		"customer_name": "魏芳",
		"product_name": "碳膏",
		"quantity": 1,
		"amount": 60,
		"buy_time": "2022-12-29 12:21:45"
	}
]

但我的真实需求是:

指定 "customer_name": "魏芳",,只 获取该用户的信息

==》 做如下改造:

class OrderSerializer(serializers.ModelSerializer):
    class Meta:
        model = Order
        fields = "__all__"
class OrderViewSet(viewsets.ModelViewSet): queryset = Order.objects.all() serializer_class = OrderSerializer
    # 按条件查询(耽误我一个星期,获取条件参数,)
    def get_queryset(self):
        return Order.objects.filter(customer=self.request.customer)

如果只是“李雷”登录,那么就只能获得 “李雷”的订单信息

[
    {
        "customer_id": 1,
        "customer_name": "李雷",
        "product_name": "车蜡",
        "quantity": 1,
        "amount": 20,
        "buy_time": "2022-12-27 22:48:48"
    },
    {
        "customer_id": 1,
        "customer_name": "李雷",
        "product_name": "车蜡",
        "quantity": 1,
        "amount": 20,
        "buy_time": "2022-12-27 23:05:20"
    },
    {
        "customer_id": 1,
        "customer_name": "李雷",
        "product_name": "车蜡",
        "quantity": 1,
        "amount": 20,
        "buy_time": "2022-12-28 08:06:27"
    },
    {
        "customer_id": 1,
        "customer_name": "李雷",
        "product_name": "车蜡",
        "quantity": 1,
        "amount": 20,
        "buy_time": "2022-12-28 08:11:57"
    },
    {
        "customer_id": 1,
        "customer_name": "李雷",
        "product_name": "车蜡",
        "quantity": 1,
        "amount": 20,
        "buy_time": "2022-12-28 08:11:59"
    },
    {
        "customer_id": 1,
        "customer_name": "李雷",
        "product_name": "车蜡",
        "quantity": 2,
        "amount": 40,
        "buy_time": "2022-12-28 08:13:08"
    },
    {
        "customer_id": 1,
        "customer_name": "李雷",
        "product_name": "车载香水",
        "quantity": 1,
        "amount": 120,
        "buy_time": "2022-12-28 08:13:58"
    },
    {
        "customer_id": 1,
        "customer_name": "李雷",
        "product_name": "车贴",
        "quantity": 1,
        "amount": 20,
        "buy_time": "2022-12-28 18:16:08"
    },
    {
        "customer_id": 1,
        "customer_name": "李雷",
        "product_name": "车贴",
        "quantity": 1,
        "amount": 20,
        "buy_time": "2022-12-28 18:16:31"
    }
]

如果当前登录用户是“魏芳”,就只得到“魏芳”的订单信息

[
    {
        "customer_id": 2,
        "customer_name": "魏芳",
        "product_name": "车蜡",
        "quantity": 1,
        "amount": 20,
        "buy_time": "2022-12-29 12:21:29"
    },
    {
        "customer_id": 2,
        "customer_name": "魏芳",
        "product_name": "车载香水",
        "quantity": 1,
        "amount": 120,
        "buy_time": "2022-12-29 12:21:36"
    },
    {
        "customer_id": 2,
        "customer_name": "魏芳",
        "product_name": "机油",
        "quantity": 1,
        "amount": 300,
        "buy_time": "2022-12-29 12:21:41"
    },
    {
        "customer_id": 2,
        "customer_name": "魏芳",
        "product_name": "碳膏",
        "quantity": 1,
        "amount": 60,
        "buy_time": "2022-12-29 12:21:45"
    }
]

# ######## 特别注意 ########

我这里是有个前置条件的:

customer=request.customer

这个玩意,也耽误我好久:
=== 》把 它做成中间件,将customer置入 request属性
# middlewares.py
from django.contrib.auth.models import User from django.core.signing import TimestampSigner from django.http import JsonResponse from apps.customer.models import Customer def check_token(get_response): # 其实就是视图函数get_response # One-time configuration and initialization. 中间件,都需要执行,可以免除未必登录用户,免费查看 def middleware(request): # 内部函数 // 中间件拦截一下,把请求交给视图 # Code to be executed for each request before # 处理request # the view (and later middleware) are called. # token->user->customer(balance) need_login = ['/api/trade/buy', '/api/trade/get_orders/', '/api/customer/get_user_info/'] if request.path in need_login: # 1.1哪个用户 token = request.headers.get('token') if not token: return JsonResponse({"code": 0, "msg": "请传递token。"}) signer = TimestampSigner(sep='.') token_user = signer.unsign_object(token) userid = token_user['userid'] user = User.objects.get(pk=userid) if not user: return JsonResponse({"code": 0, "msg": "根据token未获得有效用户。"}) # 跨表查询 customer = Customer.objects.get(user=user) if not customer: return JsonResponse({"code": 0, "msg": "未获得有效客户信息。"}) # ====》 我是谁 # 增加request的新增属性[这才是硬核] request.user = user request.customer = customer response = get_response(request) # 视图函数 经过request,之后得到一个结果 # Code to be executed for each request/response after # 处理response # the view is called. return response return middleware

需要放置到settings.py中

# settings.py
MIDDLEWARE = [   ... ...'backends.middlewares.check_token', ]

参考资料:https://www.656463.com/wenda/syyyhppdlgtjzysxcxj_529

 

posted on 2022-12-29 14:43  koolman  阅读(74)  评论(0)    收藏  举报