Reputation: 4285
I have bit complex database model structure, see below(only the structure)
class Service(models.Model)
fields .....
class ServiceBillPost(models.Model):
service = models.ForeignKey(Service, related_name='service_bill_posts')
class Payment(models.Model):
bill_post = models.OneToOneField(ServiceBillPost)
mode = models.PositiveSmallIntegerField(choices=PAYMENT_MODE, default=0)
PAYMENT_MODE = (
(0, 'Cash'),
(1, 'bKash'),
(2, 'CreditCard'),
(3, 'Cheque'),
(4, 'Company'),
)
now i want to fetch , all ServiceBillPost
of a Service
which payment mode
is company
.
i have service_id
, say 1,
then i have get that service,
service = Service.objects.get(pk=service_id)
then get all the service bill post,
service_bill_post = service.service_bill_post.all()
but here how can i filter by payment mode
of mode
field of Payment
model according to the above given relationship using django ORM?
Upvotes: 0
Views: 94
Reputation: 5958
You can use filter()
:
service = Service.objects.get(pk=service_id)
service_bill_posts = service.service_bill_posts.filter(payment__mode='Company')
Upvotes: 0
Reputation: 4512
Did you try?:
service_bill_post = service.service_bill_post.filter(payment__mode='Cash')
*Assuming:
bill_post = models.OneToOneField(ServiceBillPost, related_name='payment')
Upvotes: 1