Files
sqlquerybuilder/README.rst

48 lines
1.1 KiB
ReStructuredText
Raw Normal View History

2014-11-25 12:41:42 +01:00
SQL Query Builder based on django ORM
=====================================
What's that?
-----------
Is a library that you can use to build sql queries if your are accustomed to use Django ORM
How to use
----------
2014-11-25 12:43:38 +01:00
There are 4 main objects Q, F, QuerySet and SQLModel.
2014-11-25 12:41:42 +01:00
Using it
---------------
2014-11-25 12:43:38 +01:00
2014-11-25 12:41:42 +01:00
.. code-block:: python
2014-11-25 12:43:38 +01:00
from sqlbuilder import SQLModel, Queryset, Q, F
2014-11-25 12:41:42 +01:00
class Client(SQLModel):
table = "clients"
2014-11-25 12:43:38 +01:00
2014-11-25 12:41:42 +01:00
Client.objects.filter(name="Jhon").exclude(lastname="Doe").group_by("family")
sql = Queryset("clients").filter(name="Jhon").exclude(lastname="Doe").group_by("family")
sql = Client.objects.filter(Q(name="John") & ~Q(lastname="Doe"))
sql.group_by("family")
2014-11-25 12:43:38 +01:00
sql = Queryset("users")\
2014-11-25 12:41:42 +01:00
.filter(nombre="jose")\
.order_by( "nombre", "-fecha")\
.filter(fecha__lte=F("now()"))[:10]
2014-11-25 12:43:38 +01:00
2014-11-25 12:41:42 +01:00
"SELECT * FROM users WHERE ((nombre='jose') AND (fecha<=now())) ORDER BY nombre, fecha DESC LIMIT 10"
2014-11-25 12:43:38 +01:00
str(sql) will result an string with the sql generated
2014-11-25 12:41:42 +01:00