How to make a query that filters rows in which one column equals another one of the same table?

Say I have a model that looks like:

class StockRequest(models.Model):
    amount_requested = models.PositiveIntegerField(null=True)
    amount_approved = models.PositiveIntegerField(null=True) 

Is there any way to make a django query that would show me all requests where there is some relationship between amount_requested and amount_approved on a particular object/row?

In SQL it would be as simple as:

select * from stockrequest where amount_requested = amount_approved;

or

select * from stockrequest where amount_requested = amount_approved;

In Django, I'm not sure if it can be done, but I would imagine something like the below (NOTE: syntax completely made up and does not work).

StockRequest.objects.filter(amount_requested="__amount_approved")
8
задан Ciro Santilli 新疆改造中心法轮功六四事件 12 June 2016 в 21:26
поделиться