0

I have been assigned the task of setting up an online database with sqlite via django- where it will contain columns of information. My problem is that I can't figure out which is establishing table where the information will go- my models.py script or should I do it with a "CREATE TABLE" command? An example of the code in my models.py script is below:

 class Person(models.Model):
 firstname=models.CharField(max_length=100)
 lastname=models.CharField(max_length=100)

The tutorial that I was given recommended this command:

 insert into freezer_person (firstname,last name) values('Louis','Pasteur')

However upon executing this command I naturally got an error saying "table does not exist" i.e. I have to use the "CREATE TABLE" command. so when I tried:

 "CREATE TABLE in freezer_person(...)" 

It returned that "in" was syntactically incorrect. So my question is how do I go about creating a table that will intake info from models.py?- I can't seem to find any info on this...

asked Jan 30, 2016 at 21:03
1
  • What tutorial are you using? Is there nothing about django's migrations (or the old version syncdb)? Commented Jan 30, 2016 at 21:46

2 Answers 2

1

You don't create the tables; Django does it for you, through the migrations system.

This is all fully covered in the tutorial.

answered Jan 30, 2016 at 21:08
Sign up to request clarification or add additional context in comments.

Comments

0

Your model classes from models.py define your tables: each model class will be transposed in a table. Each property of a model class will be a column in the corresponding table. Each instance of the model class will be a row in that table.

So when you want to create a table you define a model class in the models.py file of your app, then run

python manage.py makemigrations

which tracks the changes made to the model class and generate a migration file which contains the sql statements to be applied to the database, and then to apply them to the database, you run

python manage.py migrate
answered Jan 30, 2016 at 23:33

1 Comment

Thank you for the comments. I have made all the necessary migrations and inserted values to the table- "select * from freezer_person;" however does not return anything....

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.