djangodjango-modelsvisual-studio-codedatabase-connectionmysql-cluster

how to see the table in my database after creating and migrating models in django


i tried connecting to my sql cloud cluster in visual studio code Code for settings.py is-

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME':'aras',
        'USER':'arasuser',
        'PASSWORD':'**********',
        'HOST':'mysql-numbers-0.cloudclusters.net',
        'connection':'connect1',
    }
}

Later, i created a model and run migrations . but yet i am unable to see the table in my database. am i connecting it right ?

UPDATED models.py code

from django.db import models

# Create your models here.

class category(models.Model):
    category_id = models.AutoField(primary_key=True)
    category_name=models.CharField(max_length=500)
    def __str__(self):
        return self.category_name
    

Solution

  • You can install and login in DB Browser for SQLite: https://sqlitebrowser.org/

    Go through this link.

    python sqlite use in terminal -django


    enter image description here

    models.py

     from django.db import models
     
     class Event(models.Model):
         name = models.CharField('Event Name', max_length=120)
         event_date = models.DateTimeField('Event Date')
         venue = models.CharField(max_length=120)
         manager = models.CharField(max_length=60)
         description = models.TextField(blank=True)
    

    after creating models

    python manage.py makemigrations
    

    Terminal output:

    Migrations for 'events':
      events\migrations\0001_initial.py
      - Create model Event
    

    then

    python manage.py migrate
    

    Terminal output:

    Operations to perform:
      Apply all migrations: events
    Running migrations:
      Applying events.0001_initial... OK
    

    Django uses the model to generate SQL,Try this command

    python manage.py sqlmigrate events 0001_initial
    
    BEGIN;
    --
    -- Create model Event
    --
    CREATE TABLE "events_event" (
        "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, 
        "name" varchar(120) NOT NULL,
        "event_date" datetime NOT NULL, 
        "venue" varchar(120) NOT NULL, 
        "manager" varchar(60) NOT NULL, 
        "description" text NOT NULL
    );
    COMMIT;
    

    database:

    enter image description here

    python manage.py sqlmigrate events 0001_initial

    More info : https://djangobook.com/mdj2-models/#creating-database-records