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. SQL Function Decorator by proc 3 years, 5 months ago
  2. format output as table by bendavis78 2 years, 7 months ago
  3. fast table flush without raw SQL by dsblank 2 years, 1 month ago
  4. Locking tables by miohtama 3 years, 7 months ago
  5. Drop all tables in MySQL database by mpasternacki 2 years ago

Comments

(Forgotten your password?)