I'm new to Django and I'm facing a question to which I didn't an answer to on Stackoverflow. Basically, I have 2 models, Client and Order defined as below:
class Client(models.Model): name = models.CharField(max_length=200) registration_date = models.DateTimeField(default=timezone.now) # .. class Order(models.Model): Client = models.ForeignKey(ModelA, on_delete=models.CASCADE, related_name='orders') is_delivered = models.BooleanField(default=False) order_date = models.DateTimeField(default=timezone.now) # .. I would like my QuerySet clients_results to fulfill the 2 following conditions:
- Client objects fill some conditions (for example, their name start with "d" and they registered in 2019, but it could be more complex)
- Order objects I can access by using the
ordersrelationship defined in 'related_name' are only the ones that fills other conditions; for example, order is not delivered and was done in the last 6 weeks.
I could do this directly in the template but I feel this is not the correct way to do it. Additionally, I read in the doc that Base Manager from Order shouldn't be used for this purpose. Finally, I found a question relatively close to mine using Q and F, but in the end, I would get the order_id while, ideally, I would like to have the whole object.
Could you please advise me on the best way to address this need?
Thanks a lot for your help!