django-rest-framework

How to return generated file download with Django REST Framework?

北城以北 提交于 2021-02-17 15:16:51
问题 I need to return generated file download as a Django REST Framework response. I tried the following: def retrieve(self, request, *args, **kwargs): template = webodt.ODFTemplate('test.odt') queryset = Pupils.objects.get(id=kwargs['pk']) serializer = StudentSerializer(queryset) context = dict(serializer.data) document = template.render(Context(context)) doc = converter().convert(document, format='doc') res = HttpResponse( FileWrapper(doc), content_type='application/msword' ) res['Content

How to return generated file download with Django REST Framework?

爷,独闯天下 提交于 2021-02-17 15:16:50
问题 I need to return generated file download as a Django REST Framework response. I tried the following: def retrieve(self, request, *args, **kwargs): template = webodt.ODFTemplate('test.odt') queryset = Pupils.objects.get(id=kwargs['pk']) serializer = StudentSerializer(queryset) context = dict(serializer.data) document = template.render(Context(context)) doc = converter().convert(document, format='doc') res = HttpResponse( FileWrapper(doc), content_type='application/msword' ) res['Content

How to serialize a queryset from an unrelated model as a nested serializer?

旧城冷巷雨未停 提交于 2021-02-17 09:41:30
问题 I'm trying to add a nested serializer to an existing serializer based on some criteria of the parent model, not a Foreign key. The use case is to return a 'Research' object with an array of 'ResearchTemplate' objects that are identified by filtering on a Postgres ArrayField. Models class Research(TimeStampedModel): category = models.CharField(max_length=100, choices=RESEARCH_TEMPLATE_CATEGORIES, default='quote') body = models.CharField(max_length=1000, blank=True, default='') #The body of

DRF SimpleJWT remove password and add date field

北慕城南 提交于 2021-02-17 07:10:39
问题 I created a custom user model extending from AbstractBaseUser where the only data I'm getting from the user is a userID, user (the username field) and date (required and in the format dd-mm-yyyy ) and the creation of the user works fine as you can see from the DB in the next image Used password = None and last_login = None to refer i didn't want password and last_login tables. Then, created a view where only authenticated users can access. To handle the authentication, used simpleJWT. In urls

DRF SimpleJWT remove password and add date field

会有一股神秘感。 提交于 2021-02-17 07:10:08
问题 I created a custom user model extending from AbstractBaseUser where the only data I'm getting from the user is a userID, user (the username field) and date (required and in the format dd-mm-yyyy ) and the creation of the user works fine as you can see from the DB in the next image Used password = None and last_login = None to refer i didn't want password and last_login tables. Then, created a view where only authenticated users can access. To handle the authentication, used simpleJWT. In urls

DRF SimpleJWT remove password and add date field

强颜欢笑 提交于 2021-02-17 07:10:05
问题 I created a custom user model extending from AbstractBaseUser where the only data I'm getting from the user is a userID, user (the username field) and date (required and in the format dd-mm-yyyy ) and the creation of the user works fine as you can see from the DB in the next image Used password = None and last_login = None to refer i didn't want password and last_login tables. Then, created a view where only authenticated users can access. To handle the authentication, used simpleJWT. In urls

middleware updates request.user, but request.user becomes AnonymousUser in view

元气小坏坏 提交于 2021-02-17 06:38:48
问题 I wrote a simple JWT middleware to get user from the JWT. The method get_user_from_jwt returns a User object. # app.middlewares.py class JwtMiddleware: def __init__(self, get_response): self.get_response = get_response def __call__(self, request): self.process_request(request) return self.get_response(request) def process_request(self, request): request.user = self.get_user_from_jwt(request.headers) def get_user_pk_from_jwt(self, auth_header: str) -> int: _, token = auth_header.split(' ')

Return image using Django REST framework Render

≯℡__Kan透↙ 提交于 2021-02-15 06:56:58
问题 I uploaded images using Django REST Framework . Now I'm trying to return the same image as response. views.py class ImageUploadView(viewsets.ModelViewSet): queryset = ImageModel.objects.all() serializer_class = ImageSerializer def create(self, request, *args, **kwargs): userID = (request.data.get('userID')) serializer = self.get_serializer(data=request.data) if not UserModel.objects.filter(id=userID).exists(): return Response(data={"detail": "Invalid UserID"}) else: if serializer.is_valid():

How to allow/ reject api permisions based on user group in django rest framework

时光毁灭记忆、已成空白 提交于 2021-02-11 15:36:14
问题 I have 2 groups of user Creator and viewer Creator can create,update, view , delete data while Viewer only can view data. I cant understand how to implement them easily.:Later on if Imay have to allow certain crud for different models. i feel if groups can have custom acess I will have full control , maybe a custom class I have separated the apis now need to check if group matches then allow action on the api. serializer.py from rest_framework import serializers from trackit_create.models

How to allow/ reject api permisions based on user group in django rest framework

徘徊边缘 提交于 2021-02-11 15:34:24
问题 I have 2 groups of user Creator and viewer Creator can create,update, view , delete data while Viewer only can view data. I cant understand how to implement them easily.:Later on if Imay have to allow certain crud for different models. i feel if groups can have custom acess I will have full control , maybe a custom class I have separated the apis now need to check if group matches then allow action on the api. serializer.py from rest_framework import serializers from trackit_create.models