django-rest-framework

How to put a dictionary in a JSON?

How to put a dictionary in a JSON? Question: I’m working with a REST API, and I need to return a JSON with my values ​​to it. However, I need the items of the payload variable to show all the items inside the cart_item. I have this: payload = { "items": [], } I tried …

Total answers: 2

How can get products for active owners (users) only with Django Rest Framework?

How can get products for active owners (users) only with Django Rest Framework? Question: I’m creating an e-commerce API with DRF. I would like to retrieve and display only active products from active owners (users) using a ModelViewSet How can I do this? Here is my code : views.py class ProductViewSet(viewsets.ModelViewSet): serializer_class = ProductSerializer parser_classes …

Total answers: 1

How to add array of integer field in Django Rest Framework?

How to add array of integer field in Django Rest Framework? Question: I want to add an array of integer fields in my model class Schedule(models.Model): name = models.CharField(max_length=100) start_time = models.DateTimeField(auto_now_add=True) end_time = models.DateTimeField(null=True, blank=True) day_of_the_week = ?? ( array of integer ) I tried with class Schedule(models.Model): name = models.CharField(max_length=100) start_time = models.DateTimeField(auto_now_add=True) …

Total answers: 2

Django Rest Framework, how to use serializers.ListField with model and view?

Django Rest Framework, how to use serializers.ListField with model and view? Question: I want to store an array of integers in the day_of_the_week field. for which I am using the following code models.py class Schedule(models.Model): name = models.CharField(max_length=100) day_of_the_week = models.CharField(max_length=100) serializers.py class ScheduleSerializer(serializers.ModelSerializer): day_of_the_week = serializers.ListField() class Meta(): model = Schedule fields = "__all__" …

Total answers: 1

Django 'model' object is not iterable when response

Django 'model' object is not iterable when response Question: I have 2 model. And the two models are connected to the ManyToManyField. models.py class PostModel(models.Model): id = models.AutoField(primary_key=True, null=False) title = models.TextField() comments = models.ManyToManyField(‘CommentModel’) class CommentModel(models.Model): id = models.AutoField(primary_key=True, null=False) post_id = models.ForeignKey(Post, on_delete=models.CASCADE) body = models.TextField() and serializers.py class CommentModel_serializer(serializers.ModelSerializer): class Meta: model …

Total answers: 2

Copying only non-relation attributes django/python

Copying only non-relation attributes django/python Question: I am copying a model object to another, but I want that it doesn’t copy the relations For example, assume you have a model like this: class Dish(models.Model): name = models.CharField(max_length=100) description = models.CharField(max_length=500) category = models.ForeignKey(Category, on_delete=models.CASCADE, default=1) def __str__(self): return self.name Then I do: my_dish = Dish.objects.get(pk=dish.id) …

Total answers: 3

Django Rest How to show all related foreignkey object?

Django Rest How to show all related foreignkey object? Question: I have an blog website and my visitors can also comment on my blog posts. Each blog post have multiple comment and I want to show those comment under my each single blog post. Assume Blog1 have 10 comment so all 10 comment will be …

Total answers: 1

DRF pagination issue in APIView

DRF pagination issue in APIView Question: I want to implement LimitOffsetPagination in my APIView which was successful but the url is required to be appended with ?limit=<int>. Something like this: But I do not want to manually add that endpoint. So I tried creating a new pagination.py file: But now I am not getting the …

Total answers: 1

Is there a way to add custom data into ListAPIView in django rest framework

Is there a way to add custom data into ListAPIView in django rest framework Question: So I’ve built an API for movies dataset which contain following structure: Models.py class Directors(models.Model): id = models.IntegerField(primary_key=True) first_name = models.CharField(max_length=100, blank=True, null=True) last_name = models.CharField(max_length=100, blank=True, null=True) class Meta: db_table = ‘directors’ ordering = [‘-id’] class Movies(models.Model): id = …

Total answers: 3