A good starting point for doing Django queries is the Django docs themselves.
http://docs.djangoproject.com/en/dev/topics/db/queries/
Here are a few examples:
select * from table
=
ModelName.objects.all()
filtering:
select * from table where column = 'foo'
=
ModelName.objects.filter(column='foo')
Specifically regarding the use of distinct you use the distinct() method of a Django queryset.
Here's the relevant link in the documentation.
http://docs.djangoproject.com/en/dev/ref/models/querysets/#distinct
Update:
The ORM helps you by allowing you to use Object Oriented interactions with your data. You don't write code that translates the resultset of your query into a set of objects. It does it automatically. That's the fundamental change in thought process you have to make.
You start to think in terms of 'I have this object, I need to get all the other objects which are like it' Then you can ask the ORM for those objects. ORM, I need all the objects of Class Product which have an attribute of color "blue"
Django's specific ORM language for that is:
products = Product.objects.filter(color='blue')
This is done instead of:
- writing your sql query,
- properly escaping all the arguments,
- connecting to the database,
- querying the database and handling the connection / query errors,
- getting the result set,
- iterating across the result set translating the returned values into proper objects which you can call methods on.
That's the value in using an ORM. Code simplification and reduced development time.