Home > Net >  django.core.exceptions.FieldError: Cannot resolve keyword 'date' into field. Join on '
django.core.exceptions.FieldError: Cannot resolve keyword 'date' into field. Join on '

Time:02-14

# Here is my models

This is my CustmerBuySell model DB designed.

class CustomerBuySell(models.Model):
    customer = models.ForeignKey(CustomerAdd, on_delete=models.CASCADE)
    customer_buy_sell_debit = models.DecimalField(max_digits=10, decimal_places=2, default=0.00)
    customer_buy_sell_credit = models.DecimalField(max_digits=10, decimal_places=2, default=0.00)
    description = models.CharField(max_length=250, blank=True)
    date = models.DateField()
    sms = models.BooleanField(default=False)
    picture = models.ImageField(upload_to='customer_buy_sell_pics', default='images.png')

    created_at = models.DateTimeField(auto_now_add=True, blank=True, null=True)
    updated_at = models.DateTimeField(auto_now=True, blank=True, null=True)

    def __str__(self):
        return self.customer.customer_name

    class Meta:
        verbose_name = "Customer BuySell"
        verbose_name_plural = "Customer BuySell"

# Here, is my View.

This is the class-based APIView, which I have used. And try to use the aggregate query in this view.

class DailyCustomerBuySellAPIView(APIView):

    def get(self, request):
        customer_buy_sell = CustomerBuySell.objects.extra(select={'day': 'date( date )'}).values('day').order_by(
            'date__date').annotate(available=Count('date__date'))
        serializer = CustomerBuySellSerializer(customer_buy_sell, many=True)
        return Response({"customer_buy_sell": serializer.data})


# And, finally here are my Serializers

I have no idea what's the problem! Please help me.

class CustomerBuySellSerializer(serializers.ModelSerializer):
    # customer = CustomerAddSerializer()

    class Meta:
        model = CustomerBuySell
        fields = '__all__'

    def to_representation(self, instance):
        representation = super(CustomerBuySellSerializer, self).to_representation(instance)

        if instance.customer is not None:
            customer_name = instance.customer.customer_name
            previous_due = instance.customer.previous_due
            representation['custo`enter code here`mer_name'] = customer_name
            representation['previous_due'] = previous_due
            return representation

CodePudding user response:

I suppose it is just a typo: Change date__date to date

CodePudding user response:

There are many problems with your approach. Let me mention each of them one by one:

  1. First of all remove date__date from your APIVIew

Before:

 customer_buy_sell = CustomerBuySell.objects.extra(select={'day': 'date( date )'}).values('day').order_by(
            'date__date').annotate(available=Count('date__date'))

Instead, write it as:

from django.db.models.functions import Extract

 customer_buy_sell = CustomerBuySell.objects.annotate(day=Extract('date','day')).values('day').order_by('day')

if you need a count of the days then you can try

customer_buy_sell_count = customer_buy_sell.count() 
  1. Another thing that you are doing wrong is you pass a dict to serializer as you are already using values that return a dictionary of days and not object of CustomerBuySell so you do not need to pass it to serializer otherwise you have to do it according to your need.

  2. In CustomerBuySellSerializer you are using a model serializer with __all__ while you are passing an extra fields day that is not part of it.

So in short there are so many syntax issues with your Django and Django Rest Framework.Great way to fix such issues is to set with an experience programmer so that he can improve the flow of the code. Later on you can focus on logic.

  • Related