django-rest-framework

Django REST Framework share class instance between files

老子叫甜甜 提交于 2020-12-15 04:17:46
问题 I have some code in my apps.py's ready() method that loads some Oauth providers using the authlib module. Basically I am doing this (apps.py): from django.apps import AppConfig from authlib.integrations.django_client import OAuth from django.core.cache import caches class ApiConfig(AppConfig): name = 'api' def ready(self): import api.receivers defaultcache = caches['default'] from .modules.oauth import fetch_token from .models import OauthProvider, OAuth2Token oauth = OAuth(fetch_token=fetch

Django REST Framework share class instance between files

为君一笑 提交于 2020-12-15 04:17:35
问题 I have some code in my apps.py's ready() method that loads some Oauth providers using the authlib module. Basically I am doing this (apps.py): from django.apps import AppConfig from authlib.integrations.django_client import OAuth from django.core.cache import caches class ApiConfig(AppConfig): name = 'api' def ready(self): import api.receivers defaultcache = caches['default'] from .modules.oauth import fetch_token from .models import OauthProvider, OAuth2Token oauth = OAuth(fetch_token=fetch

DRF post url without end slash

北战南征 提交于 2020-12-13 04:26:59
问题 In my application , I need to display the REST url(s) without a slash at the end. I have tried below combination but it didnt work. Added APPEND_SLASH=True in the settings.py and on the urls.py file from rest_framework.routers import SimpleRouter router = SimpleRouter(trailing_slash=False) After adding this when I am calling the urls without slash at the end in the postman, it is giving me an 404 error- URL not found. But with slash at the end is working fine. Is there any option to make this

DRF post url without end slash

北城以北 提交于 2020-12-13 04:26:05
问题 In my application , I need to display the REST url(s) without a slash at the end. I have tried below combination but it didnt work. Added APPEND_SLASH=True in the settings.py and on the urls.py file from rest_framework.routers import SimpleRouter router = SimpleRouter(trailing_slash=False) After adding this when I am calling the urls without slash at the end in the postman, it is giving me an 404 error- URL not found. But with slash at the end is working fine. Is there any option to make this

in my django project i get a 'ModelFormOptions' object has no attribute 'concrete_model' error

五迷三道 提交于 2020-12-13 04:01:13
问题 I have added a form for my django rest project. However, I keep getting a 'ModelFormOptions' object has no attribute 'concrete_model' error. In my User class I have added a new field 'password' and I was trying to create a form. and call the serializer upon it. My Model: class User(models.Model): gender = models.CharField(max_length=10, blank=False, choices=GENDER) first_name = models.CharField(max_length=20, blank=False) last_name = models.CharField(max_length=20, blank=False) position =

in my django project i get a 'ModelFormOptions' object has no attribute 'concrete_model' error

孤街醉人 提交于 2020-12-13 04:00:45
问题 I have added a form for my django rest project. However, I keep getting a 'ModelFormOptions' object has no attribute 'concrete_model' error. In my User class I have added a new field 'password' and I was trying to create a form. and call the serializer upon it. My Model: class User(models.Model): gender = models.CharField(max_length=10, blank=False, choices=GENDER) first_name = models.CharField(max_length=20, blank=False) last_name = models.CharField(max_length=20, blank=False) position =

Converting JSON response observable to angular (typescript) object array

安稳与你 提交于 2020-12-12 05:51:25
问题 I have a service API that pulls data from Django-REST. The returned JSON looks like: [ { "manufacturer": "Mfg", "model": "Model", }, { "manufacturer": "Mfg2", "model": "Model2", } ] The service API function getData returns: return this.httpClient.get(`${this.apiURL}/data/`); Note I am using ListAPIView in Django, and I have verified that the above URL returns the JSON object shown above (though I have to add ?format=json to get the raw data outside of Django's APIView). I then have an angular

different queryset based on permissions in Django Rest Framework

怎甘沉沦 提交于 2020-12-10 16:52:18
问题 I have seen this link, but I didn't find anything related to my question helping it being resolved. Imagine we have to create a blog, in which posts have two status: is_draft published ( published == !is_draft ) So, each user should see all of his/her posts, whether it is draft or not. In addition, Other users should see the published posts of rest of the users. I am using viewsets in django and I know that we should have different queryset based on the current user permissions but I don't

Django Rest Framwork get user in serializer

余生长醉 提交于 2020-12-06 12:56:16
问题 I can get the user in my serializer? For example I have this serializer: serializers.py class ClientSearchSerializer(serializers.ModelSerializer): client = serializers.SlugRelatedField( many=False, queryset=Client.objects.filter(user=????), slug_field='id' ) I can filter my queryset from SlugRelatedField? 回答1: CurrentUserDefault should work for you. Documentation: http://www.django-rest-framework.org/api-guide/validators/#currentuserdefault Please note: In order to use this, the 'request'

Django Rest Framwork get user in serializer

痴心易碎 提交于 2020-12-06 12:56:10
问题 I can get the user in my serializer? For example I have this serializer: serializers.py class ClientSearchSerializer(serializers.ModelSerializer): client = serializers.SlugRelatedField( many=False, queryset=Client.objects.filter(user=????), slug_field='id' ) I can filter my queryset from SlugRelatedField? 回答1: CurrentUserDefault should work for you. Documentation: http://www.django-rest-framework.org/api-guide/validators/#currentuserdefault Please note: In order to use this, the 'request'