I have the following table with the following records:
|id | datetime | state_id | brand_id | customer_id | recommender_id |
|---|----------|----------|----------|-------------|----------------|
| 1 | ... | 5 | 1 | 32 | 31 |
| 2 | ... | 5 | 1 | 32 | 19 |
| 3 | ... | 5 | 9 | 32 | 8 |
| 4 | ... | 5 | 28 | 32 | 8 |
| 5 | ... | 6 | 1 | 32 | 8 |
The entity in the model is called Recommender . brand_id
and customer_id
are foreign keys of User .
And I'm designing a query using Django 2
that returns all the records where the customer_id is equal to the logged user ( request.user
) and the state is equal a 5. This is my query and it works:
recommend = Recommender.objects.all().filter(
customer=request.user, authorized=State.objects.get(pk=5)
)
The previous query returns all the records in state 5 (with customer = request.user
).
However, I need that when a pair (brand_id, customer_id) has state 6 (like the last record in the previous table), I do not return in the query no record of that couple in state 5.
How could I make that query using the Django ORM?