如何只允许对当前用户的数据进行POST和GET请求?

时间:2019-04-25 12:58:34

标签: django authentication django-views django-queryset

我只想显示每个用户的数据。因此,一个用户无法获取或发布另一位用户的数据。

class FcaSet(viewsets.ModelViewSet):

    def get_queryset(self): 
        return Fca.objects.filter(user_check= request.user) #user filter
    serializer_class = FcaSerializer
    permission_classes = (permissions.AllowAny,)

但通常会引发错误:/ api / cutarea-fca /中的TypeError int()参数必须是字符串,类似字节的对象或数字,而不是'AnonymousUser'

接下来,我尝试使用此功能:

def get_queryset(self): 
        return Fca.objects.filter(user_check= request.user)

但是用户在前端看不到任何来自此模型的对象...

它是模型:

class Fca(models.Model):
    num_of_agree = models.ForeignKey(Agreement, models.DO_NOTHING, blank=True, null=True)
    uroch = models.TextField(blank=True, null=True)
    num_allot = models.ForeignKey(Allotment, models.DO_NOTHING, blank=True, null=True)
    num_fca = models.CharField(max_length=50, blank=True, null=True)
    ar_fca = models.DecimalField(max_digits=8, decimal_places=2, blank=True, null=True)
    expl_ar = models.FloatField(blank=True, null=True)
    cel_nazn = models.ForeignKey(ForestPurpose, on_delete=models.DO_NOTHING, verbose_name='task',blank=True, null=True) 
    cat_zas = models.ForeignKey(ForestProtection, on_delete=models.DO_NOTHING, verbose_name='cate',blank=True, null=True ) 
    geom = models.MultiPolygonField(geography=True, null=True, blank=True, verbose_name='ge')
    video_cat = models.TextField(blank=True, null=True)
    user_check = models.ForeignKey(User,on_delete=models.DO_NOTHING, verbose_name='user_id',blank=True, null=True )




    class Meta:
        managed = True
        verbose_name = 'lek'
        verbose_name_plural =  'leks'

有序列化器:

class FcaSerializer(gis_serializers.GeoFeatureModelSerializer):

    class Meta:
        model = Fca
        geo_field = 'geom'
        fields = ('id','num_of_agree','num_allot','uroch','num_fca','ar_fca',\
       'expl_ar','cel_nazn','cat_zas','geom','video_cat','user_check')

    def create(self, validated_data):
        return Fca.objects.create(**validated_data)

我希望有人通过简单的示例或psedo代码发短信说明如何解决这一经典任务

0 个答案:

没有答案