user2607925
user2607925

Reputation: 103

setup a database in django

I followed the django doc and went through the polls example. I have a sqlite db 'nameinfodb'. I want to access it by search last name online. I setup models.py as

class Infotable(models.Model):
    pid_text = models.CharField(max_length=200)
    lname_text = models.CharField(max_length=200)
    fname_text = models.CharField(max_length=200)
    affs_text = models.CharField(max_length=2000)
    idlist_text = models.CharField(max_length=2000)
    def __str__(self):
        return self

I copied name.info.db to where db.sqlite3 locates, changed settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'name.info.db'),
    }
}

Then I run

python manage.py migrate  
python manage.py makemigrations pidb

Then I checked if I did correctly

$ python manage.py sqlmigrate pidb 0001

BEGIN;
--
-- Create model Choice
--
CREATE TABLE "pidb_choice" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "choice_text" varchar(200) NOT NULL, "votes" integer NOT NULL);
--
-- Create model Question
--
CREATE TABLE "pidb_question" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "question_text" varchar(200) NOT NULL, "pub_date" datetime NOT NULL);
--
-- Add field question to choice
--
ALTER TABLE "pidb_choice" RENAME TO "pidb_choice__old";
CREATE TABLE "pidb_choice" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "choice_text" varchar(200) NOT NULL, "votes" integer NOT NULL, "question_id" integer NOT NULL REFERENCES "pidb_question" ("id"));
INSERT INTO "pidb_choice" ("id", "question_id", "choice_text", "votes") SELECT "id", NULL, "choice_text", "votes" FROM "pidb_choice__old";
DROP TABLE "pidb_choice__old";
CREATE INDEX "pidb_choice_7aa0f6ee" ON "pidb_choice" ("question_id");

COMMIT;

I didn't see any information about the new database, all I see is the example polls data. Did I do anything wrong here? All I need to do is connect to name.info.db, and access it by last name. Thanks!

Upvotes: 2

Views: 70

Answers (1)

yuvi
yuvi

Reputation: 18447

You need to do it the other way around:

python manage.py makemigrations pidb
python manage.py migrate

See this part of the tutorial for more information

Upvotes: 3

Related Questions