daily pastebin goal
87%
SHARE
TWEET

Untitled

a guest Jan 22nd, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Service(models.Model):
  2.     name = models.CharField(max_length=255, blank=True, null=True)
  3.     class Meta:
  4.         managed = True
  5.         db_table = 'Service'
  6.  
  7. class Room(models.Model):
  8.     name = models.CharField(max_length=255, blank=True, null=True)
  9.     service = models.ForeignKey(Service, models.DO_NOTHING, blank=True,
  10.     null=True)
  11.     class Meta:
  12.         managed = True
  13.         db_table = 'Room'
  14.    
  15. class ServiceSerializer(serializers.ModelSerializer):
  16.     room_set = RoomSerializer(many=True, read_only=True)
  17.     class Meta:
  18.        model = Service
  19.        fields = ('name','room_set')
  20. class RoomSerializer(serializers.ModelSerializer):
  21.     class Meta:
  22.         model = Room        
  23.         fields = '__all__'
  24.    
  25. queryset = Service.objects.filter(room__id=1)
  26. serializer = ServiceSerializer(queryset, many=True)
  27. return JsonResponse(serializer.data, safe=False)
  28.    
  29. {
  30.      "name": "Hotel1",
  31.      "room_set": [
  32.      {
  33.            "id": 1,
  34.            "name": "Room1"
  35.       },
  36.    
  37. {
  38.      "name": "Hotel1",
  39.      "room_set": [
  40.      {
  41.            "id": 1,
  42.            "name": "Room1",
  43.       },
  44.       {
  45.             "id": 2,
  46.             "name": "Room2",
  47.       },
  48.       {
  49.              "id": 3,
  50.              "name": "Room3",
  51.       }
  52.   }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top