model instance to sql insert statement

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
def _object_to_query(obj):
    values = [(f, '\'%s\'' % f.get_db_prep_save(f.value_from_object(obj))) for f in obj._meta.local_fields]
    q = sql.InsertQuery(obj)
    q.insert_values(values)
    compiler = q.get_compiler('default')
    # Normally, execute sets this, but we don't want to call execute
    setattr(compiler, 'return_id', False)
    stmt, params = compiler.as_sql()
    stmt = stmt % params
    return stmt 

More like this

  1. Bulk Insert - updated 5/9/2008 by coolie 5 years, 7 months ago
  2. MySQL "Text" Type Model Field by blackbrrr 4 years, 12 months ago
  3. Arbitrary auto-generated primary keys by gsakkis 2 years, 10 months ago
  4. Changing field type in production by lawgon 6 years, 1 month ago
  5. PowerDNS models by diverman 3 years, 12 months ago

Comments

(Forgotten your password?)