django-rest-framework

How to add search parameters to GET request in Django REST Framework?

这一生的挚爱 提交于 2021-02-07 09:20:45
问题 After having read through and completed the Django REST Framework tutorial, it is not completely obvious how one would implement a filter on a GET request. For example, ListAPIView is great for viewing all the instances of a Model in your database. However, if I wanted to limit the results (e.g. for a Book model, I might want to limit the results by publication date or author, etc.). It seems the best way to do this would be to create a custom Serializer, View, etc. and basically write

Django rest framework imagefield optional

可紊 提交于 2021-02-07 07:33:10
问题 models.py picture = models.ImageField(upload_to='Images/', default='/Images/None/No-img.jpg', blank=True,null=True) serializers.py class UserProfileSerializer(serializers.ModelSerializer): picture = serializers.ImageField(max_length=None, allow_empty_file=True, use_url=True) class Meta: model = UserProfile fields = ('id','picture',) read_only_fields=('id') But it's still showing as a required field, I can't make post request with image, is it a bug? 回答1: Maybe you should try setting the field

Django Rest Framework update data before serialize

时光怂恿深爱的人放手 提交于 2021-02-07 05:59:08
问题 I want that any user could create their own products with my RESTful API. For this purpose, I created a view that inherits ListCreateAPIView. The problem is that a user should only create products that he/she own, so when the instance of the Product model is created, I wanted that the field Owner corresponds to the user that is authenticated. Here's my Product model class Product(models.Model): owner = models.ForeignKey(User) name = models.CharField(max_length=30) Obviously my serializer is:

Django Rest Framework debug post and put requests

[亡魂溺海] 提交于 2021-02-07 05:17:53
问题 I use DRF extension to se json list for model, and there i can debug with debug-toolbar that GET request, but how can i debug POST and PUT requests? I have this for settings in debug mode: INSTALLED_APPS += ('debug_toolbar',) MIDDLEWARE_CLASSES += ('debug_toolbar.middleware.DebugToolbarMiddleware',) DEBUG_TOOLBAR_PATCH_SETTINGS = False INTERNAL_IPS = ( '127.0.0.1' ) Now, when i try with Intercept redirects in debug-toolbar, it doesn't show me toolbar when i do POST . 回答1: I found django-silk

Django Rest Framework debug post and put requests

一曲冷凌霜 提交于 2021-02-07 05:16:00
问题 I use DRF extension to se json list for model, and there i can debug with debug-toolbar that GET request, but how can i debug POST and PUT requests? I have this for settings in debug mode: INSTALLED_APPS += ('debug_toolbar',) MIDDLEWARE_CLASSES += ('debug_toolbar.middleware.DebugToolbarMiddleware',) DEBUG_TOOLBAR_PATCH_SETTINGS = False INTERNAL_IPS = ( '127.0.0.1' ) Now, when i try with Intercept redirects in debug-toolbar, it doesn't show me toolbar when i do POST . 回答1: I found django-silk

remove authentication and permission for specific url path

断了今生、忘了曾经 提交于 2021-02-06 12:59:48
问题 I'm working with DRF and came across this issue. I have a third-party view which I'm importing in my urls.py file like this : from some_package import some_view urlpatterns = [ path('view/',some_view) ] but the issue I'm facing is since I have enabled default permission classes in my settings.py like this: REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework.authentication.TokenAuthentication', ), 'DEFAULT_PERMISSION_CLASSES':( 'rest_framework.permissions.IsAuthenticated', )

remove authentication and permission for specific url path

帅比萌擦擦* 提交于 2021-02-06 12:58:25
问题 I'm working with DRF and came across this issue. I have a third-party view which I'm importing in my urls.py file like this : from some_package import some_view urlpatterns = [ path('view/',some_view) ] but the issue I'm facing is since I have enabled default permission classes in my settings.py like this: REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework.authentication.TokenAuthentication', ), 'DEFAULT_PERMISSION_CLASSES':( 'rest_framework.permissions.IsAuthenticated', )

Django Rest Framework custom permissions per view

别等时光非礼了梦想. 提交于 2021-02-06 12:44:46
问题 I want to create permissions in Django Rest Framework, based on view + method + user permissions. Is there a way to achieve this without manually writing each permission, and checking the permissions of the group that the user is in? Also, another problem i am facing with, is that permission objects are tied up to a certain model. Since i have views that affect different models, or i want to grant different permissions on the method PUT, depending on what view i accessed (because it affects

migrations are not taking place after pushing my code on heroku…?

送分小仙女□ 提交于 2021-02-05 11:34:09
问题 Well its been 12 hours and i'm still unable to deploy my project properly. I just dont know what is wrong happening. You guys can see throughing error that such models not exist . But i'm trying to make migrations my heroku run python manage.py makemigrations and it throughing me this traceback. My code is working perfectly fine in local but on the server side this is happening. Please help me out. Traceback (most recent call last): File "manage.py", line 22, in <module> main() File "manage

Data is not being saved as Encrypted data django

扶醉桌前 提交于 2021-02-05 10:39:47
问题 till now I tried more then 6 plugins and quite frustrate now. Now using this one cryprtography everything is fine and done accordingly but when I save data in model manager like this def create_user(self, email, password, **extra_fields): user = self.model(email=email, **extra_fields) user.test_field = 'new.user@oc.com' user.save(using=self._db) return user it saving data normally not encrypted My model is like class User(AbstractBaseUser, PermissionsMixin): email = models.EmailField(max