Category : django-rest-framework

Consider I have models, a serializer and a viewset as below: # models.py class Foo(models.Model): owner = models.ForeignKey(User, models.CASCADE, related_name="bars") # serializers.py class FooSerializer(serializers.ModelSerializer): owner = serializers.PrimaryKeyRelatedField( default=serializers.CurrentUserDefault(), queryset=User.objects.all(), ) class Meta: fields = ["pk", "owner"] # viewsets.py # registered to /api/foos/ class FooViewSet(viewsets.ModelViewSet): serializer_class = FooSerializer permission_classes = [permissions.IsAuthenticated] queryset = Foo.objects.all() So, when ..

Read more

I need to filter model with (datetime.now() < end) My model class Quiz(models.Model) : title = models.CharField(max_length=50) start = models.DateTimeFieldField(default="2021-10-10") end = models.DateTimeFieldField(default="2021-10-11") My view class GetQuizView(generics.ListAPIView) : def get_queryset(self): now = datetime.now() return Quiz.objects.filter(start = now) serializer_class = QuizListSerializer But I can only filter with equal time, I can’t use > or < Source: ..

Read more

I want to pass user_id from view to serializer I have model Answer class Answer(models.Model) : text = models.CharField(max_length=500) question_id = models.CharField(max_length=25) user_id = models.CharField(max_length=25, default=1) This is my Serializer class CreateAnswer(generics.CreateAPIView) : def get_serializer_context(self): context = super().get_serializer_context() context["id"] = self.request.user.id return context serializer_class = AnswerQuestionSerializer queryset = Answer.objects.all() What I need to write in ..

Read more

this always gives None request_ticker = request.POST.get(‘tick’) views.py class DataClassView(APIView): authentication_classes = [] permission_classes = [] def post(self, request, format=None): request_ticker = request.POST.get(‘tick’) print(request_ticker) #this always print None (my problem) #I want to get ICRA here which I am passing from ajax stk = Stocks.objects.filter(ticker=’ITC’) stock_ser_obj = StocksSerialize(stk, many=True) return Response(stock_ser_obj.data) here is my ajax ..

Read more

I was using Django users model for my Django rest framework. For this I used Django’s ModelViewSet for my User class. class UserViewSet(viewsets.ModelViewSet): queryset = User.objects.all() serializer_class = UserSerializer Serializers.py class UserSerializer(serializers.ModelSerializer): class Meta: model = User fields = [‘id’, ‘username’, ‘password’] extra_kwargs = { ‘password’ : { ‘write_only’:True, ‘required’: True } } def create(self, ..

Read more

I am very new to django, any help highly appreciated. thanks in advance! Here is my code ‘model.py’ class Stocks(models.Model): ticker = models.CharField(max_length=30, primary_key=True, unique=True) company_name = models.CharField(max_length=100, blank=True, null=True) sector = models.CharField(max_length=50, blank=True, null=True) class Meta: db_table = ‘stocks’ def __str__(self): return "%s %s %s" % (self.ticker, self.company_name, self.sector) class QuarterlyFinance(models.Model): ticker = models.ForeignKey(Stocks, ..

Read more

I’m quite new to DRF and I trying to build a system with Document and Category. These are models.py for it class TimeStampedModel(models.Model): created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) class Meta: abstract = True class Document(TimeStampedModel): unique_id = models.UUIDField(default=uuid.uuid4, editable=False, unique=True) title = models.CharField(max_length=50, blank=True, default="") description = models.TextField(blank=True, default="") image = models.ImageField(blank=True, null=True) category ..

Read more