使用外键的Django Rest框架过滤器

时间:2018-09-07 13:07:23

标签: django django-models django-rest-framework django-filter

我具有以下3个表的数据库结构,如下所示

餐桌人物

Id  U_id    PersonName
101 12       Iron Man
201 12       Spider Man
301 15       Thanos

表EarnTypes

Id  U_id    TypeEarning
10  12       Salary
20  15       Lottery
30  15       Gambling

表收入条目

ID     U_Id P_Id    EarnType_Name   Earn_Amt    Earn_Date
1001    12  101         10           5$         8-Jun-2017
3001    15  301         20           25$        7-Apr-2018
4001    12  201         10           50$        19-Apr-2018

我的列表API查看代码遵循以下规则,我仅根据登录用户过滤数据。

    class EarningEntryAPIView(mixins.CreateModelMixin,generics.ListAPIView):
        permission_classes      = [IsOwnerOnly]
        serializer_class        = EarningsSerializer
        #
        def get_queryset(self):
            request = self.request
            #print (request.user)
            qs = EarningsEntry.objects.filter(U_id=self.request.user)
            query = request.GET.get('q')
            if query is not None:
                qs = qs.filter(Earning_Type_id__EarningTypeName__contains=query)
            return qs

        def post(self,request,*args,**kwargs):
            return self.create(request,*args,**kwargs)

        def perform_create(self, serializer):
            serializer.save(id=self.request.user)

我的EarningEntry模型如下。

def upload_file(instance, filename):
    return "earnings/{user}/{filename}".format(user=instance.id, filename=filename)


class EarningsQuerySet(models.QuerySet):
    def serialize(self):
        list_values = list(
            self.values('Id', 'U_id', 'P_id', 'Earning_Type_id', 'Ear_Amt', 'Ear_Img', 'Ear_date', 'Ear_comm'))
        print(list_values)
        return json.dumps(list_values, sort_keys=True, indent=1, cls=DjangoJSONEncoder)


class EarningssManager(models.Manager):
    def get_queryset(self):
        return EarningsQuerySet(self.model, using=self._db)


# Create your models here.
class EarningsEntry(models.Model):
    Id = models.AutoField(primary_key=True)
    U_id=models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
    P_id =models.ForeignKey(personmodel.Person,on_delete=models.CASCADE)
    Earning_Type_id = models.ForeignKey(eartypemodel.EarningTypes,on_delete=models.CASCADE)
    Ear_Amt = models.FloatField(null=False,blank=False)
    Ear_Img = models.ImageField(null=True,blank=True)
    Ear_date = models.DateField("ExpenseDate",null=False,blank=False)
    Ear_comm = models.TextField()
    objects = EarningssManager()

    def __str__(self):
        return str(self.U_id) + str(self.P_id) + str(self.Ear_Amt) + str(self.Ear_Img) +str(self.Ear_Img)+str(self.Earning_Type_id)+ \
               str(self.Ear_date) + str(self.Ear_comm) + str(self.Id) or ""

    def serialize(self):
        data={
            'Id': self.Id,
            'U_id': self.U_id,
            'P_id': self.P_id,
            'Earning_Type_id': self.Earning_Type_id,
            'Ear_Amt': self.Ear_Amt,
            'Ear_Img': self.Ear_Img,
            'Ear_date': self.Ear_date,
            'Ear_comm': self.Ear_comm
        }
        data = json.dumps(data,sort_keys=True,indent=1,cls=DjangoJSONEncoder)
        return data


    @property
    def owner(self):
        return self.Id

现在,当我尝试浏览用于插入或列表视图的api时,我会获得U_id,P_id和Earnig_Type_id外键引用的所有值,这是不正确的。

如何仅根据已登录的用户进行过滤。在当前视图中,我正在查看所有内容。

请参考此屏幕,其中nirav和nirav13是两个不同的用户。 enter image description here

1 个答案:

答案 0 :(得分:2)

首先,请勿使用大写字母命名字段。其次,不要在外键名称中输入“ _id”,请看django docs。无论如何,您应该覆盖__init__并为这些字段过滤查询集:

class EarningsSerializer(serializer.Modelserializer):
    def __init__(self, *args, **kwargs):
        super(EarningsSerializer, self).__init__(*args, **kwargs)
        request_user = self.context['request'].user
        self.fields['P_id'].queryset = Person.objects.filter(U_id=request_user)
        self.fields['Earning_Type_id'].queryset = EarningTypes.objects.filter(U_id=request_user)