django-rest-framework

DRF Viewset remove permission for detail route

瘦欲@ 提交于 2020-12-30 08:28:23
问题 I have a basic Viewset: class UsersViewSet(viewsets.ModelViewSet): permission_classes = (OnlyStaff,) queryset = User.objects.all() serializer_class = UserSerializer It is bind to the /api/users/ endpoint. I want to create a user profile page, so I need only a particular user, so I can retrieve it from /api/users/<id>/ , but the problem is that I want /api/users/<id>/ to be allowed to anyone, but /api/users/ to keep its permission OnlyStaff , so no one can have access to the full list of users

Django Rest framework Serialize many to many field

邮差的信 提交于 2020-12-29 07:12:10
问题 I am trying to serialise a json payload that has a field with an array, the .is_valid() check is returning true but I am getting KeyError: 'passengers' when I try to do this serializer.data['passengers'] but the other fields work fine (such as booking_number and status). This is the response.data I am passing to the seralizer: {'booking_number': 2839, 'passengers': [{'first_name': 'Jack', 'surname': 'Smith', 'email': 'smith@mail.com', 'phone_number': '1234'}], 'status': 'ON_HOLD'} My

Django Rest framework Serialize many to many field

风格不统一 提交于 2020-12-29 07:10:48
问题 I am trying to serialise a json payload that has a field with an array, the .is_valid() check is returning true but I am getting KeyError: 'passengers' when I try to do this serializer.data['passengers'] but the other fields work fine (such as booking_number and status). This is the response.data I am passing to the seralizer: {'booking_number': 2839, 'passengers': [{'first_name': 'Jack', 'surname': 'Smith', 'email': 'smith@mail.com', 'phone_number': '1234'}], 'status': 'ON_HOLD'} My

Django Rest framework Serialize many to many field

爷,独闯天下 提交于 2020-12-29 07:07:31
问题 I am trying to serialise a json payload that has a field with an array, the .is_valid() check is returning true but I am getting KeyError: 'passengers' when I try to do this serializer.data['passengers'] but the other fields work fine (such as booking_number and status). This is the response.data I am passing to the seralizer: {'booking_number': 2839, 'passengers': [{'first_name': 'Jack', 'surname': 'Smith', 'email': 'smith@mail.com', 'phone_number': '1234'}], 'status': 'ON_HOLD'} My

Django How to Serialize from ManyToManyField and List All

為{幸葍}努か 提交于 2020-12-29 07:01:32
问题 I'm developing a mobile application backend with Django 1.9.1 I implemented the follower model and now I want to list all of the followers of a user but I'm currently stuck to do that. I also use Django Rest Framework. This is my UserProfile model class UserProfile(models.Model): # Linking UserProfile to User model. user = models.OneToOneField(User) city = models.CharField(null=True, max_length=30, blank=True) gender = models.CharField(null=True, max_length=10, blank=True) # m for male, f for

Django How to Serialize from ManyToManyField and List All

六月ゝ 毕业季﹏ 提交于 2020-12-29 06:59:04
问题 I'm developing a mobile application backend with Django 1.9.1 I implemented the follower model and now I want to list all of the followers of a user but I'm currently stuck to do that. I also use Django Rest Framework. This is my UserProfile model class UserProfile(models.Model): # Linking UserProfile to User model. user = models.OneToOneField(User) city = models.CharField(null=True, max_length=30, blank=True) gender = models.CharField(null=True, max_length=10, blank=True) # m for male, f for

Django How to Serialize from ManyToManyField and List All

核能气质少年 提交于 2020-12-29 06:58:46
问题 I'm developing a mobile application backend with Django 1.9.1 I implemented the follower model and now I want to list all of the followers of a user but I'm currently stuck to do that. I also use Django Rest Framework. This is my UserProfile model class UserProfile(models.Model): # Linking UserProfile to User model. user = models.OneToOneField(User) city = models.CharField(null=True, max_length=30, blank=True) gender = models.CharField(null=True, max_length=10, blank=True) # m for male, f for

DRF: custom ordering on related serializers

梦想的初衷 提交于 2020-12-29 05:25:08
问题 I've got two serializers one for an Instructor and one for a Course. The Instructor serializer has a defined relationship to the courses like so: courses = CourseSerializer(many=True, read_only=True) How do I specify an order to the courses in the instructor serializer? Note: I have defined the ordering value in the Course Meta class, but it is the default sorting I want everywhere else. What I want is a custom ordering for api output (specifically dept, number, -semester rather than the

Adding claims to DRF simple JWT payload

冷暖自知 提交于 2020-12-26 13:50:20
问题 Using djangorestframework_simplejwt library, when POST to a custom view #urls.py path('api/token/', MyTokenObtainPairView.as_view(), name='token_obtain'), #views.py class MyTokenObtainPairView(TokenObtainPairView): serializer_class = MyTokenObtainPairSerializer I'm able to get a the following access token eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNTkwOTEwNjg0LCJqdGkiOiI3M2MxYmZkOWNmMGY0ZjI3OTY4MGY0ZjhlYjA1NDQ5NyIsInVzZXJfaWQiOjExfQ

Adding claims to DRF simple JWT payload

£可爱£侵袭症+ 提交于 2020-12-26 13:32:06
问题 Using djangorestframework_simplejwt library, when POST to a custom view #urls.py path('api/token/', MyTokenObtainPairView.as_view(), name='token_obtain'), #views.py class MyTokenObtainPairView(TokenObtainPairView): serializer_class = MyTokenObtainPairSerializer I'm able to get a the following access token eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNTkwOTEwNjg0LCJqdGkiOiI3M2MxYmZkOWNmMGY0ZjI3OTY4MGY0ZjhlYjA1NDQ5NyIsInVzZXJfaWQiOjExfQ