Reputation: 53
I have a lot of standard services with different prices in different ServicePlaces.
My models.py:
class Service(models.Model):
service = models.CharField(max_length=200, unique=True)
class ServicePlace(models.Model):
place = models.CharField(max_length=200, unique=True)
#what services are provided in this ServicePlace:
service = models.ManyToManyField('Service')
class Price(models.Model):
price = models.DecimalField(max_digits=6, decimal_places=2)
service = models.ForeignKey("Service")
place = models.ForeignKey("Place")
How I can get all services with prices for the ServicePlace X ?
Upvotes: 2
Views: 315
Reputation: 599836
You have a direct relationship from ServicePlace to Service, so you can just follow that:
services = service_place_x.service.all()
(Note your service
field is misnamed, because it refers to multiple objects, so it should probably be called services
.)
From there, you can access the price directly on each service:
for service in services:
price = service.price
This incurs an extra db hit on each iteration though, so you probably want to use select_related
on the original query:
services = service_place_x.service.all().select_related('price')
Upvotes: 0