django.core.exceptions.FieldError: Cannot resolve keyword 'date' into field. Join on 'date' not permitted

django.core.exceptions.FieldError: Cannot resolve keyword 'date' into field. Join on 'date' not permitted

# Here is my models

这是我设计的CustmerBuySell模型数据库。

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.

这是我用过的基于class的APIView。并尝试在此视图中使用聚合查询。

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

我不知道有什么问题!请帮助我。

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

我想这只是一个打字错误:将 date__date 更改为日期

你的方法有很多问题。让我一一提及:

  1. 首先从您的 APIVIew
  2. 中删除 date__date

之前:

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

而是写成:

from django.db.models.functions import Extract

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

如果您需要计算天数,那么您可以尝试

customer_buy_sell_count = customer_buy_sell.count() 
  1. 你做错的另一件事是你将字典传递给序列化程序,因为你已经在使用 values 那 return 天的字典而不是 [= 的对象14=] 所以你不需要将它传递给序列化器,否则你必须根据你的需要来做。

  2. CustomerBuySellSerializer 中,您正在使用带有 __all__ 的模型序列化程序,而您正在传递一个不属于它的额外字段日。

简而言之,您的 Django 和 Django Rest 存在很多语法问题 Framework.Great 解决此类问题的方法是与经验丰富的程序员一起设置,以便他可以改进代码的流程。以后你可以专注于逻辑。