Relative Content

Tag Archive for djangodjango-rest-framework

nesting a serializer by year then month

class Transactions(models.Model): id = models.CharField(max_length=100, primary_key=True) owner = models.ForeignKey(User, on_delete=models.CASCADE, null=True) date = models.DateField() watch = models.CharField(max_length=100) purchase_price = models.IntegerField() sale_price = models.IntegerField() My Transactions model has a date field, purchase price, and sale_price. @api_view([‘GET’]) @permission_classes([IsAuthenticatedOrReadOnly]) def profit_chart(request): current_year = datetime.now().year queryset = Transactions.objects.filter(date__year=current_year).values(month=ExtractMonth(‘date’), year=ExtractYear(‘date’)).annotate( profit=Sum(F(‘sale_price’)) – Sum(F(‘purchase_price’))).order_by(‘month’) serializer = ProfitChartSerializer(queryset, many=True) return Response(serializer.data, status=status.HTTP_200_OK) […]

Django Axes Lockout Not Working as Expected

I’m using Django Axes to lock out users after a certain number of failed login attempts. However, despite my configurations, users are still able to log in immediately after being locked out if they enter the correct credentials. I want to ensure that users are locked out for 1 hour, regardless of whether they enter the correct credentials during that period.

Unable to update user profile using PUT method (Django DRF)

I am currently working on a mentor-mentee matching service using Django DRF. I have made a ‘users’ app and have completed creating users&logging in. But I can’t edit user info using PUT method.
Here are my project files (models.py, serializers.py, views.py, urls.py, settings.py)
1. models.py

how to add product to my cart in django rest frame work drf

this is my model class Cart(models.Model): id = models.UUIDField(primary_key=True, default=uuid4) created_at = models.DateTimeField(auto_now_add=True) class CartItem(models.Model): cart = models.ForeignKey(Cart, on_delete=models.CASCADE, related_name=’items’) product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name=’cart_items’) quantity = models.PositiveSmallIntegerField() class Meta: unique_together = [[‘cart’, ‘product’]]