0

I have a room model which has a many to one relation with a venue.

A venue can have many rooms.

I am trying to set up my http methods in my rest framework so that way when I add permissions things work well.

So if say someone wants to delete a room from a venue, I must make sure that 1 that person has permissions to that venue 2 that room is attached to that venue

I would like to get a venue model then get the venue models room_set and check the room_set to see if a room exists with the room primarykey of the model I wish to delete.

What I have so far:

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({})

my hope is I could just interate venue rooms and check each object in venue rooms but I have a strong feeling its not going to work because venuerooms is not python objects? Perhaps it is. I will be updating this question after I do the for loop or possibilly deleting it if I find everything in working order.

My question is how do I get the room set and check to see if a room with the roompk I am searching for is in it.

so as i expected I got an error the code I attempted:

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({})

the error i got:

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

any ideas of what I could do?

new edit:

so I implemented the answer below and added all() to my venuerooms definition

but now I have a new error. This one doesn't make sense as I am clearly overriding the None attribute in the for loop.

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({})

error:

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
  • check my answer, you are trying to access the room_set object, but you should be accessing room_set.all() (which is iterable) – Walucas Mar 14 '18 at 20:01
  • @Walucas hey thank you it did help, I just implemented it but it looks like I still have a problem I am editing my question now to account for it. –  Mar 14 '18 at 20:03

2 Answers2

0

Add your models to help the question.... but here is how you access the 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({})
Walucas
  • 2,549
  • 1
  • 21
  • 44
  • this is still good, but now we must write an exception in case the filter does not find the roompk. Its essically my answer but calling django methods. What I like about mine is I have logic added to deal with not finding the roompk in the room list. Thank you though for your efforts! Youre helping me in a big way! –  Mar 14 '18 at 20:23
0

so I ended up doing this:

grabbing the room object once I found it in the roomset. by querying the database for the room object. I am not happy with this answer though. As I would imagine the room_set already has access to the room object? Can't I just delete it that way? If I can I would appreciate a better answer than mine. Regardless here is what I did.

 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)
  • This is a really bad approach. It involves possibly hundreds of database lookups when only a single one is actually required. – Daniel Roseman Mar 14 '18 at 21:13
  • @DanielRoseman then what do you propose that also allows me a way to check if the user has permissions to the venue? I don't see a better solution from you, just criticism. –  Mar 14 '18 at 22:01