David542
David542

Reputation: 110492

Using raw sql in django

What 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?

Upvotes: 0

Views: 892

Answers (2)

Sam Dolan
Sam Dolan

Reputation: 32542

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.

Upvotes: 2

Kevin Pullin
Kevin Pullin

Reputation: 13337

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.

Upvotes: 2

Related Questions