开发者

Using raw sql in django

开发者 https://www.devze.com 2023-03-02 20:43 出处:网络
Wh开发者_如何学Goat would be the equivalent raw sql for the following: def index: Emails.objects.create(email=request.POST[\'invite_email\'])

Wh开发者_如何学Goat would be the equivalent raw sql for the following:

def index:
    Emails.objects.create(email=request.POST['invite_email'])

I have this so far, but I can't quite get the quotations working --

    cursor = connection.cursor()
    cursor.execute("insert into splash_emails (id, email) values ('0','request.POST[invite_email]')")
    transaction.commit_unless_managed()

What would be correct way to write this, and is this the simplest way to perform raw sql?


If you ever want to see the queries django is using you can do:

emails = Emails.objects.create(email=request.POST['invite_email'])
print emails.query

It's a bit verbose, but you'll get the gist.


I think after reading the Django cookbook chapter on Security, you'll have a good idea on how to execute raw sql AND execute it safely.

0

精彩评论

暂无评论...
验证码 换一张
取 消