How to access the request.user in a Piston classmethod

前端 未结 2 380
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-14 03:11

I have a model which contains a ManyToMany to User to keep track of which users have \'favorited\' a particular model instance.

In my API for this model, when reques

2条回答
  •  孤独总比滥情好
    2021-01-14 03:57

    From the piston wiki page it says that you may specify the contents of foreign keys and many to many fields by nesting attributes. In your case

    class FriendHandler(BaseHandler):
        allowed_methods = ('GET',)
        model = User
        fields = ('userfield_1', 'userfield_2', ('friends', ('is_friended')))
    
        def read(self, request):
            # Anything else you might want to do, but return an object of type User
            # Or whatever your model happens to be called
    

    EDIT: Another slightly hacky way to do it (if you don't want the friend to get passed at all if the is_friended is false) would be to manually create a dict object structured how you like, and then return it. piston processes the dict a works with the built in emitters (the JSON one for sure, haven't tried the others)

提交回复
热议问题