Django custom managers - how do I return only objects created by the logged-in user?

前端 未结 3 1387
粉色の甜心
粉色の甜心 2020-11-28 22:19

I want to overwrite the custom objects model manager to only return objects a specific user created. Admin users should still return all objects using the objects model mana

3条回答
  •  無奈伤痛
    2020-11-28 23:16

    It seems necessary to use the middleware to store the user information.

    However, I'd rather not modify the default ModelManager objects, but hook it upto a different manager, that I will use in the code, say in your case user_objects instead of objects.

    Since you will use this only within views that are @login_required you dont need all the complex error handling in the Middleware.

    Just my 2¢.

提交回复
热议问题