class Room (models.Model):
restaurant = models.ForeignKey(Restaurant,verbose_name='Restaurant', on_delete=models.CASCADE)
room_name = models.CharField(max_length=50,verbose_name='Room Name')
def __str__(self):
return self.room_name
class Table (models.Model):
room = models.ForeignKey(Room,on_delete=models.CASCADE)
capacity = models.IntegerField()
name = models.CharField(max_length=30,verbose_name='Table Name')
def __str__(self):
return self.room.room_name + ': ' + self.name
I'm using django to build an API and I want to return these models as a JSON. The problem is that when I return a Room object the Tables are not in the JSON. How can I serialise a Room and also get the tables serialised with it.
This is how I'm serialising the Room:
rooms = Room.objects.filter(restaurant=id)
return HttpResponse(serializers.serialize("json", rooms))
The problem is that the reverse relationship is not being included.