0

I have a table which not defined as a Model in Django server. So I need to use raw query to create object in Database

My database:

Table name: msg_rooms

enter image description here

I want to create object { with_user: 5, room_name: 'abc', user_id: 2 } by using raw query in Django.

halfer
  • 19,824
  • 17
  • 99
  • 186
KitKit
  • 8,549
  • 12
  • 56
  • 82

1 Answers1

2

The raw query is only for model related queries. However you can use something like

from django.db import connection

with connection.cursor() as cursor:
    connection.execute(
        "INSERT INTO msg_rooms(with_user, room_name, user_id) "
        "VALUES (%s, %s, %s)", [5, 'abc', 2])

You can read more details on https://docs.djangoproject.com/en/2.1/topics/db/sql/#executing-custom-sql-directly

J. Ghyllebert
  • 2,009
  • 1
  • 29
  • 35