366 questions
0
votes
1
answer
66
views
How should I define redirect request properly
I have a social network project for users to share their recipes. The Recipe objects managed by a ViewSet, which has Serializer, where create and update recipes operations defined, via POST and PATCH ...
0
votes
2
answers
136
views
Why is authenticate in drf not working and how do I make it work?
I am using Django with rest framework. Sign Up works fine, but when I log in, after passing the email and password to the backend, the authenticate() returns None even if the credentials are right. I ...
0
votes
0
answers
76
views
Django Serializer - `methodViewSet(viewsets.ViewSet)` is not displayed in url list
I am trying to combine multiple serializers into a single API call, and as a result, I am using viewsets.ViewSet. I am aware that viewsets.ModelViewSet automatically generates the routes, but with ...
0
votes
1
answer
84
views
Why does my Blog API CustomUser have an error when sending request for a CustomUser detail?
I have two Blog API's in Django Rest Framework. One uses Django's default User model whilst the other uses a CustomUser. Authentication works well in both cases. I have similar serializers, viewsets ...
1
vote
0
answers
58
views
Django REST ModelViewSet view returns 404 after PATCH
I have ModelViewSet view in django rest framework project. I am trying to filter documents that are not live, I mean are not on main view. To filter I am using params: http://localhost:8000/documents?...
1
vote
2
answers
61
views
Overwrite django rest response for return only pk
I'm trying to overwrite the response that is sended by django when create a new object.
I need to return only the primary key of the created object instead the whole object.
The function that I'm ...
0
votes
1
answer
38
views
How do I create/update/retrieve multiple Models in one View in Django Rest Framework
I have a Business model and Order model. Businesses can order furniture in our website. A user creates an account and makes an order. Since he is a new user, he is not associated to any Business. In ...
0
votes
2
answers
79
views
Django restframework user email already exists error
I am trying to write a login view in django restframe work to authenticate a user into the application but each time I send a request to the server I keep getting this error
`{
'email': ['email ...
0
votes
1
answer
406
views
Using ViewSet, Generics and Mixin together in Django REST Framework
I just started learning about DRF. We know that with generics and mixin Django REST Framework makes handling GET, POST, PUT, DELETE etc easy for users. And another feature is ViewSets. It helps us to ...
1
vote
1
answer
327
views
Fetching one object using DRF retrieve viewset without pk or uuid
I'm designing an api using python DRF for a merchant profile page. ID should not be required to be passed into the api.
I'm trying to do a simple fetch to get one object to return as a result and I ...
1
vote
1
answer
38
views
Getting a serializer KeyError on a non-required field
I am hitting a KeyError for 'children' when I run this create() function:
class TaskViewSet(ModelViewSet):
queryset = Task.objects.all()
serializer_class = TaskSerializer
def create(self, ...
0
votes
1
answer
160
views
In Django, where does the "data" field come from in the Serializer class?
I see in a bunch of django code, especially in viewsets, that when serializer is initialized like the following:
class UserViewSet(viewsets.ModelViewSet):
"""
A viewset that ...
0
votes
1
answer
101
views
Django Rest Framework generic views, so clunky to get the object/instance
Often I wish to use the created/updated object, for say to log it in some history, or use it in an email.
E.g.1. CreateAPIView
def create(self, request, *args, **kwargs):
ret = super()....
0
votes
2
answers
60
views
DRF requires the user to be logged in to use login_user api
I am trying to create a CustomUserViewset, and add a login_user api to it. The problem is that although I set the permission_classes to AllowAny, still when calling the login_user api, it says: {"...
1
vote
0
answers
209
views
Django REST Framework Viewset Executes Actions Multiple Times and Shows 'None' for GET Requests
I have a Django REST framework viewset with multiple actions such as list, create, and more. When I send a GET request, I noticed that some actions are executed multiple times, and the action is ...
0
votes
1
answer
96
views
Redirect to ViewSet list view from update causes PUT method not allowed
Expanding on the Django REST Framework tutorial, I have switched to TemplateHTMLRenderer to work with HTML.
class SnippetViewSet(viewsets.ModelViewSet):
queryset = Snippet.objects.all()
...
0
votes
1
answer
193
views
Object of type CharField is not JSON serializable problem
Creating my first CRUD in Django Rest framework this error keeps popping up. Does anyone know how to fix?? Object of type CharField is not JSON serializable
models:
class Pessoa(models.Model):
id =...
0
votes
1
answer
79
views
How to filter models by currently authenticated user using ModelViewSet in DRF
I'm working on Django project with Django Rest Framework. I have a model called Server and I want to filter the queryset by the currently authenticated user, but only if "by_user=true" is ...
1
vote
1
answer
214
views
DRF prefetch related still causing N+1 queries
Models:
class GradePolicy(models.Model):
name = models.CharField(max_length=30)
minScore = models.DecimalField(default=0, max_digits=4, decimal_places=1, db_column="minscore")
...
0
votes
1
answer
66
views
ValueError: Cannot query "User.name": Must be "Model" instance
I am facing an issue with querying the Profile model based on the user_id of a Project instance in my Django REST framework project. When attempting to retrieve the profile using the user_id, I ...
0
votes
1
answer
47
views
Why serializer data isn't displayed by foreign key in view?
I need to show user info in my post as nested json.
Models:
Profile
# Django
from django.db import models
class Profile(HelperModel):
""" User Profile
UserProfile provides the ...
1
vote
1
answer
236
views
How can I serialize a list of objects and return the object if validated?
I have the below in one of my viewset classes:
serializer = ResponsesSerializer(data=queryset, many=True)
serializer.is_valid(raise_exception=True)
validated_data = serializer....
1
vote
0
answers
93
views
DRF: pass a variable from the router to the ViewSet
I'm working with Django REST Framework and I want to reuse a ViewSet in two places, but to do that I need to pass some information to the ViewSet class.
This is what my urls.py file currently looks ...
0
votes
0
answers
144
views
Challenges Accessing List Action in Class Viewset with Retrieve-only Endpoint
I have a class viewset that only performs the retrieve action. I have a challenge trying to access the endpoint if a retrieve action is not included in the viewset. The URL for this endpoint takes in ...
0
votes
1
answer
106
views
Why am I getting a TypeError when passing HTTP method dictionaries to Django REST Framework's as_view() method in ViewSets?
I created a ViewSet, in which I need to use in two URL Patterns, one is for getting the list of items and other is for getting the detail of single item. The problem is, when I pass {"get": &...
1
vote
0
answers
39
views
AssertionError: The field 'uploaded_file' was declared on serializer LessonPlanNotesSerializer, but has not been included in the 'fields' option
My Models
class LessonPlan(models.Model):
planner=models.ForeignKey('LessonPlanner', on_delete=models.CASCADE)
title=models.CharField(max_length=100, null=True, blank=True)
details=models.TextField(...
1
vote
1
answer
192
views
Using http methods with django rest framework
I'm looking for some advice on how to correctly use my DRF API I have built, specifically the PATCH method at present. I am trying to formulate a script to patch the quantity of a Product / Cart Item ...
0
votes
1
answer
61
views
Split a single drf viewset class into multiple classes?
Right now I have a large viewset class (~1300 lines). The reason for this is, I had to add a lot of @action decorators to acheive my goal. But I can feel that this is taking a lot of my time in ...
1
vote
1
answer
187
views
The endpoint is throwing me a KeyError when I try to update it
Pardon me, I'm developing an app using Django REST Framework and when I try to update an user this happen
It throw me this error:
centros = validated_data.pop('centros')
KeyError: 'centros'
I'll share ...
-2
votes
1
answer
460
views
Getting localhost for next page url in production
hlo, I have deployed a Django rest app in production. When I call a list API there is a pagination, and I am getting localhost for next page URL.
I am using GenericViewSet and LimitOffsetPagination ...
0
votes
0
answers
31
views
Join two models when only 1 model has a foreign key
ModelA
------
id
atr_a
atr_b
atr_c
is_active
ModelB
------
id
atr_d
atr_e
modelA_id
Without modifying either model, I need to include ModelB data in my ModelA ViewSet. I am not sure how this should ...
1
vote
1
answer
142
views
Django Rest UpdateView: This field is required
I have the following view:
class CampaignUpdate(generics.RetrieveUpdateAPIView):
queryset = Campaign.objects.all()
serializer_class = CampaignSerializer
permission_classes = [...
0
votes
1
answer
31
views
rest_framework returns Not found on url users/1/
I need to return via rest_framework a users's detail
In my views.py i have UserViewSet class with
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = ...
1
vote
1
answer
1k
views
What is the difference between @action and @api_view to create routes in Django Rest Framework
Both decorators appear to simply create a route for you. But what would be the practical difference (if any)? In the documentation, it is not clear.
I even created two routes the same way and there ...
0
votes
0
answers
653
views
How to Override CreateModelMixin corrctly
I want to override CreateModelMixin with my own class which derived from ModelViewSet
this is the class i want to override
class CreateModelMixin:
"""
Create a model instance.
...
0
votes
1
answer
167
views
Cannot resolve keyword into field error while using drf model serializer and search_fields within field set?
SerializerClass:
class VacancySerializer(serializers.ModelSerializer):
organization_small_name = serializers.CharField(source='organization.short_name', read_only=True)
class Meta:
...
0
votes
1
answer
81
views
How to count a number of users in a particular event
I am trying to count the number of users in my event name field. But I am facing problems in django-rest-framework filtering.
I tried different logics, but I cannot count. I got the count when I hard ...
0
votes
1
answer
477
views
Calling a DRF View from another View
I have a User Model Viewset that adds users to my Django Rest Framework application (VueJS -> DRF -> PostGres).
I have another ModelViewSet for an activity log that has entries when users get ...
0
votes
1
answer
204
views
change an object with ModelViewSet
im building a project with DRF and im trying to understand how the modelviewset is working,
im sending a url like this:
localhost/do_somthing/object_id/
and route it with :
router.register('...
-1
votes
1
answer
166
views
get and delete in Django framework
i want to build a ModelViewSet class that recive an id from url for instance
localhost/id
and based on that id i can either show the object with matching id or delete the object
but im having ...
1
vote
1
answer
150
views
Post is working in model view set, with functions name post
class Product_List(viewsets.ModelViewSet): # >>> List of products & add product to cart
permission_classes = [IsAuthenticated, ]
queryset = Item.objects.all()
...
1
vote
1
answer
1k
views
How do I use permission_classes in a custom method of a Viewset in DjangoRestFramework?
Suppose that I have a Viewset named UserViewset, and I have assigned IsAuthenticated permission to UserViewset Viewset. Now, I want to create a normal method (not an action method), and I want to ...
0
votes
1
answer
739
views
Why does a retrieve request end up calling `get_queryset( )`?
The DRF helps you make a simple API that exposes an endpoint for listings of an object and the details for a specific object.
My viewset looks like this:
class UserViewSet(ModelViewSet):
...
2
votes
2
answers
367
views
self.request.user not returning in queryset
In my views, queryset is returning all the users when I want it to be only returning the user that is currently logged. I have a get self method which has the serializer set to the user but it is not ...
0
votes
1
answer
1k
views
Is there a way to have an action with detail = True inside another action with detail = True in Django rest?
My user model has a Many to one relationship to a Clothes model and inside my User viewset I created an extra action to list the Clothes instances in relation to a specific user
@action(detail=True, ...
0
votes
1
answer
117
views
Call is-valid when pagination is applied to data in DRF
I have a ModelViewSet and I am using ModelSerializer. I am also using Pagination that is from django.core.
My views code snippet that works:
page_number = request.data.get("page")
data = ...
0
votes
1
answer
41
views
Viewsets: the input field for Tag object does not appear, can not create Dot object with post method
Not sure of the reason but maybe I have passed in a wrong way request to the context in views.py, In views.py where post method 'tag' input field is missing
class Dot(models.Model):
user =...
3
votes
2
answers
4k
views
How do I specify a custom lookup field for a DRF action on a viewset?
I would like to specify a custom lookup field on the action (different from the viewset default "pk"), i.e.
@action(
methods=["GET"],
detail=True,
url_name=&...
0
votes
0
answers
92
views
Django path didn’t match any of these when adding query param to the endpoint
I am using django 4.1
I have a product model
class Product(models.Model):
product_id = models.CharField(
blank=False,
null=False,
max_length=50,
verbose_name="...
1
vote
1
answer
396
views
How to create a statistics endpoint?
I'm learning DRF and I've been stuck on this for a few days. I'm trying to create an endpoint that receives a date range .The response should return a report with the monthly sales distribution for ...