如何检查模型是否在模型中设置了密钥集? Django,django休息框架

时间:2018-03-14 19:51:19

标签: django django-rest-framework django-related-manager

我有一个房间模型,与场地有多对一的关系。

场地可以有很多房间。

我正在尝试在我的rest框架中设置我的http方法,这样当我添加权限时,一切正常。

因此,如果有人想要从场地中删除一个房间,我必须确保这一点 1该人有权访问该场所 2该房间附属于该场地

我想获得一个场地模型 然后获取场地模型room_set并检查room_set以查看是否存在我希望删除的模型的房间主键的房间。

到目前为止我所拥有的:

class GetEditDeleteVenueRoom(APIView):

    def get(self, request, *args, **kwargs):
        pass

    def post(self, request, *args, **kwargs):
        print('wait its over here')



    def delete(self, request, *args, **kwargs):
        venuepk = kwargs.get('venuepk', None)
        venue = get_object_or_404(Venue, pk=venuepk)
        venuerooms = venue.room_set
        print(venuerooms)
        return Response({})

我的希望是我可以只在场地房间进行交互并检查会场室中的每个物体,但我有一种强烈的感觉它不能工作,因为venuerooms不是python对象?也许是这样。我将在执行for循环后更新此问题,或者如果我发现所有内容都处于正常工作状态,则可能会删除它。

我的问题是如何获得房间设置并检查是否有一个房间里有我正在搜索的房间。

所以我预料到我尝试的代码出错:

def delete(self, request, *args, **kwargs):
    venuepk = kwargs.get('venuepk', None)
    venue = get_object_or_404(Venue, pk=venuepk)
    venuerooms = venue.room_set
    roompk = kwargs.get('roompk')
    roomobject = None
    for room in venuerooms:
        if room.pk == roompk:
            roomobject = room
    roomobject.delete()

    print(venuerooms)
    return Response({})

我得到的错误:

File "/home/rickus/Documents/softwareProjects/211hospitality/suitsandtables/backend/virtualsuits/suitsandtables/venues/views.py", line 125, in delete
    for room in venuerooms:
TypeError: 'RelatedManager' object is not iterable

我能做什么的任何想法?

新编辑:

所以我实施了下面的答案并将all()添加到我的venuerooms定义

但现在我有了一个新错误。这个没有意义,因为我明显覆盖了for循环中的None属性。

def delete(self, request, *args, **kwargs):
        venuepk = kwargs.get('venuepk', None)
        venue = get_object_or_404(Venue, pk=venuepk)
        venuerooms = venue.room_set.all()
        roompk = kwargs.get('roompk')
        roomobject = None
        for room in venuerooms:
            if room.pk == roompk:
                print(room)
                roomobject = room
        roomobject.delete()

        print(venuerooms)
        return Response({})

错误:

File "/home/rickus/Documents/softwareProjects/211hospitality/suitsandtables/backend/virtualsuits/suitsandtables/venues/views.py", line 129, in delete
    roomobject.delete()
AttributeError: 'NoneType' object has no attribute 'delete'
[14/Mar/2018 20:01:53] "DELETE /api/suitsadmin/venue/1/room/15 HTTP/1.1

2 个答案:

答案 0 :(得分:0)

添加模型以帮助解决问题....但这是您访问room_set的方式

def delete(self, request, *args, **kwargs):
    venuepk = kwargs.get('venuepk', None)
    venue = get_object_or_404(Venue, pk=venuepk)
    roompk = kwargs.get('roompk')
    room = venue.room_set.filter(pk=roompk)
    room.delete()
    return Response({})

答案 1 :(得分:0)

所以我最终这样做了:

一旦我在房间里找到它,就抓住房间里的物体。通过查询数据库中的房间对象。我对这个答案不满意。我可以想象room_set已经可以访问房间对象了吗?我不能这样删除它吗?如果可以,我会感谢比我更好的答案。无论我做了什么。

 def delete(self, request, *args, **kwargs):
        venuepk = kwargs.get('venuepk', None)
        venue = get_object_or_404(Venue, pk=venuepk)
        venuerooms = venue.room_set.all()
        roompk = kwargs.get('roompk')
        roomobject = None
        for room in venuerooms:
            if room.pk == roompk:
               roomobject = Room.objects.get(pk=roompk)
               roomobject.delete()
               return Response({})

        return Response(status.HTTP_404_NOT_FOUND)