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.
Got KeyError when attempting to get a value for field `date` on serializer
Im trying to do a queryset to show date__month and total of all sale_prices
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’]]
Django decorator and middleware issue
My decorator looks like this
how to autogenerate context for APIView methods
I need to write my own APIView class which overrides drf’s APIView and it will have autogenerated self.context which I can pass into serializer.
For example
`class SomeView(APIView):
def post(self, request):
why can not post this data in rest_framework django?
I’m encountering an error when testing this code with Postman. Can you help me identify the cause? “AssertionError at /POST_Create_NewInvoiceBuy/
Django FOREIGN KEY constraint failed when registering new users
I’m encountering a “FOREIGN KEY constraint failed” error while trying to register new users in my Django application. Here’s the relevant code from my models.py, serializers.py, and views.py files:
Django REST Framework Route Not Appearing in API List
I’m facing an issue with Django REST Framework where a specific route is not appearing in the API list when I access https://myserver.com/api/. Here’s my setup: