i'm new into Python/Django programming and i got stuck with something in a personal project that i'm doing. My issue is that i want to return a custom response based on different models of my application, some of the values will come from custom queries and others are part of the models itself.
So, i have the following models in my app(some fields were deleted to not make the post too long):
class Parking(models.Model):
google_id = models.CharField(max_length=100)
short_name = models.CharField(max_length=100)
long_name = models.CharField(max_length=300)
price = models.DecimalField(max_digits=4, decimal_places=2, null=True, blank=True)
class ParkingLot(models.Model):
parking = models.ForeignKey(Parking, on_delete=models.CASCADE, null=False, related_name='parkinglots')
size = models.ForeignKey(Size, on_delete=models.DO_NOTHING, null=False, related_name='size')
width = models.DecimalField(max_digits=3, decimal_places=2, null=True, blank=True)
height = models.DecimalField(max_digits=3, decimal_places=2, null=True, blank=True)
class ParkingAvailability(models.Model):
parkinglot = models.ForeignKey(ParkingLot, on_delete=models.CASCADE, null=False, related_name='availability')
available = models.BooleanField(null=False, blank=False)
from_hour = models.TimeField(auto_now=False, auto_now_add=False, default='09:00:00')
to_hour = models.TimeField(auto_now=False, auto_now_add=False, default='21:00:00')
These models are an exact representation of my database tables. All good. My problem is that now i want to make a custom json response, this needs to run queries over these tables but not show the entire objects and in some cases, custom fields based on filter or operations that i need to run over the tables (for example, numbers of parkinglots based on a parking_id and size_id). So, let's assume that i need something that will look like this:
[
{
"id": 1,
"google_id": "JSDKKLAD888737283",
"short_name": "Fernandez Albano",
"long_name": "Comunidad Fernandez Albano",
"price": 18.5,
"parkinglots": 88,
"hours": {
"from_hour": "09:00:00",
"to_hour": "21:00:00",
}
}
]
The first 4 json values come from Parking model, the field parkinglots is the .count() of the parkinglots model with some filters (parking_id, size_id). Availability is a custom query from ParkingAvailability model that i can access through my parkinglot_id value.
So far, i have something like this but it seems to me that is not ok:
class parkingList(APIView):
def get(self, request, format=None):
parkinglotNumber=ParkingLot.objects.filter(parking_id = 2, size_id = 1).count()
parkinglot = ParkingLot.objects.filter(parking_id = 2, size_id = 1)
hours = ParkingAvailability.objects.filter(parkinglot__in=parkinglot, available=True).aggregate(Min('from_hour'), Max('to_hour'))
content = {
'parkinglots_number': parkinglotNumber,
'hours': hours
}
return Response(content)
So, my questions are:
- It's ok to run queries like that? If want to add (for example) my parkinglot model entirely to that response i will need to run another query like "ParkingLot.objects.filter(parking_id = 2, size_id = 1)" and add it to my parking response?
- My serializer serialize all the models fields but in this case i don't use it, it's ok to return this with no serializers class?
- How i can build complex json response? Like in this case, i need to add different part to my json from different models and custom fields.
Thanks for your help! Sorry for my English but is not mi native language.
PS: My serializer class return all the information of my models. A part of the class it's like this:
class ParkingLotSerializer(serializers.ModelSerializer):
size = SizeSerializer(many=False, read_only=True)
availability = ParkingAvailabilitySerializer(many=True, read_only=True)
class Meta:
model = ParkingLot
fields = ['id', 'price', 'floor', 'number', 'width', 'size', 'availability']
class ParkingSerializer(serializers.ModelSerializer):
adress = AdressSerializer(many=False, read_only=True)
parkinglots = ParkingLotSerializer(many=True, read_only=True)
class Meta:
model = Parking
fields = ['id', 'google_id', 'price', 'short_name', 'long_name', 'adress', 'parkinglots' ]