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...
2 Answers 2
You don't create the tables; Django does it for you, through the migrations system.
This is all fully covered in the tutorial.
Comments
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
1 Comment
Explore related questions
See similar questions with these tags.
syncdb)?