django-rest-framework

Django Rest Framework: URL for associated elements

百般思念 提交于 2021-02-08 11:53:25
问题 I have the following API endpoints already created and working fine: urls.py: router = DefaultRouter() router.register(r'countries', views.CountriesViewSet, base_name='datapoints') router.register(r'languages', views.LanguageViewSet, base_name='records') Now, I need to create a new endpoint, where I can retrieve the countries associated with one language (let's suppose that one country has just one associated language). For that purpose, I would create a new endpoint with the following URL

Django, Django REST Framework, Internet Explorer and CSRF token missing or incorrect

三世轮回 提交于 2021-02-08 11:24:21
问题 We have a Django App using REST Framework. It is a nginx, redis, celery, gunicorn and PostgreSQL setup. There is only one App server. Our ajax calls use this function : $.ajaxSetup({ beforeSend: function (jqXHR, settings) { ... jqXHR.setRequestHeader("X-CSRFToken", secureCheck.reqCSRFToken()); }) if ($currentForm.attr('method') != 'POST') { if ($currentForm.attr('method') != '') { var typeRequest = $currentForm.attr('method'); headersRequest= {'X-HTTP-Method-Override': typeRequest}; } } var

How to handle to GET request in in same FBV(function based View.)

我与影子孤独终老i 提交于 2021-02-08 10:40:19
问题 Hello Everyone I am a beginner in Django and I have created one webpage where the user is able to search/Filter results and for this, I have used the Django-filter concept. Now my requirement is user should be able to download that filtered data. To achieve this I have created one separate View class with a download name and trying to pass the same Filter Class Queryset but with no luck, it is giving me all model data (records). Please find the Post: how to use Django filtered class data to 2

How to map to request.post json key to model serialializer field in django?

主宰稳场 提交于 2021-02-08 09:21:48
问题 I am newbie in djnago-rest-framework . I am leaning about creating instance with serializer in DRF . Let suppose I have models who look like this (models.py) : from django.db import models class Reporter(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=30) email = models.EmailField() class Article(models.Model): headline = models.CharField(max_length=100) reporter = models.ForeignKey(Reporter, on_delete=models.CASCADE) My serializer look like

Django REST framework: how to respond with useful error messages with get_queryset()

巧了我就是萌 提交于 2021-02-08 07:41:36
问题 I have a django model which I want to display via Django Rest framework. I am getting all objects in the model to be displayed via the get_queryset() . However, I also have a couple of query_params which will filter out certain objects. This is my main code which is working fine: class PlanView(generics.ListAPIView): """ API endpoint which allows prices to be viewed or edited """ serializer_class = PlanSerializer permission_classes = (IsAuthenticatedOrReadOnly,) # override method def get

How to set Timeout for Django Rest HTTP request

和自甴很熟 提交于 2021-02-08 06:26:19
问题 I am using Python 3.5, Django 1.9.5 and Django Rest Framework 3.3.3. I have only few POST apis in my server. How can I set Timeout for each rest http request that if the execution for a request takes more then 3 minutes than it should return timeout in response and should stop execution of that request. Thanks :) 回答1: I think you will need to set this at the proxy/web server level. With nginx you can use proxy_read_timeout: proxy_read_timeout 180s; Or you could set it at the application

How to set Timeout for Django Rest HTTP request

ⅰ亾dé卋堺 提交于 2021-02-08 06:26:09
问题 I am using Python 3.5, Django 1.9.5 and Django Rest Framework 3.3.3. I have only few POST apis in my server. How can I set Timeout for each rest http request that if the execution for a request takes more then 3 minutes than it should return timeout in response and should stop execution of that request. Thanks :) 回答1: I think you will need to set this at the proxy/web server level. With nginx you can use proxy_read_timeout: proxy_read_timeout 180s; Or you could set it at the application

Django multiple pks in url

对着背影说爱祢 提交于 2021-02-08 05:47:19
问题 I am currently trying to build an API using the Django Rest Framework. Currently I want to be able to have multiple pks in a single url, but when I try I get an error: django.core.exceptions.ImproperlyConfigured: "^video/(?P[0-9]+)/quiz/(?P[0-9]+)/list/$" is not a valid regular expression: redefinition of group name 'pk' as group 2; was group 1 at position 31 Here are my URLS: url(r'^video/(?P<pk>[0-9]+)/quiz/(?P<pk>[0-9]+)/list/$', views.QuizList.as_view(), name='quizzes-list'), url(r'^video

Staff-only permissions in Django Rest Framework

荒凉一梦 提交于 2021-02-08 05:41:45
问题 I am trying to create Django Rest Framework ModelViewSets that are staff only. When i try to use the standard Django decorator @staff_member_required I get errors which make me believe the decorators won't work with Django Rest Framework. So I am trying to write my own ModelViewSet mixin. It mostly works as I want, except for the update method which I can't make work. So, two questions: is there a more elegant way to do this, and if not, what is wrong with my update method? I can't find any

Staff-only permissions in Django Rest Framework

限于喜欢 提交于 2021-02-08 05:41:30
问题 I am trying to create Django Rest Framework ModelViewSets that are staff only. When i try to use the standard Django decorator @staff_member_required I get errors which make me believe the decorators won't work with Django Rest Framework. So I am trying to write my own ModelViewSet mixin. It mostly works as I want, except for the update method which I can't make work. So, two questions: is there a more elegant way to do this, and if not, what is wrong with my update method? I can't find any