3

As I used the SQL Server, Django automatically creates and uses schema dbo for any tables.

Is there any way to use existing schema, or force Django to use create another schema instead of all under dbo schema.

For example:

from django.db import models
import pyodbc
# Create your models here.

class Meetup(models.Model):
    title = models.CharField(max_length=255)
    slug = models.SlugField(unique=True)
    description = models.TextField()

Django has automatically created the table in my SQL Server database

This is my settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'sql_server.pyodbc',
        'NAME': "Django",
        'USER': '',
        'PASSWORD': '',
        'HOST': "DESKTOP-VA0DOPJ",
        'PORT': '',
        'OPTIONS': {
            'driver': 'ODBC Driver 17 for SQL Server',
            'isolation_level': 'READ UNCOMMITTED',  # prevent SELECT deadlocks
        },
    },
}
Dale K
  • 25,246
  • 15
  • 42
  • 71
Long Vu
  • 31
  • 1
  • 1
    Does this answer your question? [django-pyodbc-azure - How to use schemas](https://stackoverflow.com/questions/35358322/django-pyodbc-azure-how-to-use-schemas) – SMor Jun 25 '21 at 11:08
  • Thank you, it is. It can be done with the schema in settings. I found out that it's very useful then. – Long Vu Dec 30 '21 at 08:50
  • Using mssql - not sure if this will be of help. https://learn.microsoft.com/en-us/samples/azure-samples/mssql-django-samples/mssql-django-samples/ I found this article useful for accessing existing schema - perhaps it could be used to implement new schema. Note that the mssql needs to be added to the `INSTALLED APPS` for the below to work. Works with Django 4.0.5. Then add a meta class below your model class: `db_table='[schema].[table]'` https://github.com/microsoft/mssql-django/issues/56 – R_100 Jun 13 '22 at 08:24

0 Answers0