custom sql without table names

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
    # dict indexed to model names
    tables = {'str':'%s'}
    for x in ModelOne, ModelTwo:
        tables[x._meta.object_name] = x._meta.db_table

    cursor = connection.cursor()
    ex_str =  """\
    select field1, field2, sum(field3) from %(ModelOne)s where date between %(str)s and %(str)s 
    group by field1, field2
    """ % tables
    cursor.execute(ex_str, dates)
    sumt = cursor.fetchall()

More like this

  1. PostgreSQL ON DELETE CASCADE by mjt 3 years, 6 months ago
  2. post_migrate handler to load initial SQL after migrating with south by stingydrone 2 years, 4 months ago
  3. Jquery ajax csrf framework for Django by chriszweber 1 year, 4 months ago
  4. CustomQueryManager by zvoase 4 years, 10 months ago
  5. Custom SQL via subquery by drg006 6 years ago

Comments

(Forgotten your password?)