Django starting from zero \ (part: 1 ) -Qiita
Creating your first Django app, part 2|Django documentation| Django
mysite/setting.py --Python module that describes various settings of jango --Database support etc. are included by default
INSTALLED_APPS
Settings for important parts of the application. Details below
--django.contrib.admin
--Administration (admin) site
--django.contrib.auth
--Authentication system
--django.contrib.contenttypes
--Contenttypes framework
--django.contrib.sessions
--Session framework
--django.contrib.messages
--Message framework
--django.contrib.staticfiles
--Static file management framework
Use the following command to create a database table.
python manage.py migrate
-The migrate command
refers to the INSTALLED_APPS settings above and creates all the required databases according to the database settings in the` mysite / setting.py' file.
--It is possible to display the contents of the table with the command line client.
- \dt (PostgreSQL
- SHOW TABLES; (MySQL)
- .schema (SQLite)
- SELECT TABLE_NAME FROM USER_TABLES; (Oracle)
from django.db import models
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
--Generating two models, Question and Choice --Both are subclasses of the models class --Each class represents the database feel of the model
--CharFiled, IntegerField, etc. specify what data type to store
--Conditions and initial values can be specified, such as models.CharField (max_length = 200)
and models.IntegerField (default = 0)
--Foreign Key defines a relationship with Question
.
--Add the setting to ʻINSTALLED_APPS in
setting.py' to include the application in your project
--Set the class described in ʻapps.py in the application (this time Polls) to ʻINSTALLED_APPS
INSTALLED_APPS = [
'polls.apps.PollsConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
Run the code below to save Django's changes in the form of a migration
python manage.py makemigrations polls
python manage.py check
Command to check if there is a problem with the project
After creating the model table, execute migrate again to create the model table in the database.
--Change model --Run python manage.py make migrations to create migrations --Run python manage.py migrate to apply these changes to the database
--To commit the migration to the validation management system and distribute it with the app --Easy development ――Because it will be easy to use for other developers and production environments
If you do >>> Question.objects.all ()
in the shell
The output is as follows.
<QuerySet [<Question: Question object (1)>]>
I don't know what the database tables look like
So add the `str'method to both Question and Choice
from django.db import models
class Question(models.Model):
# ...
def __str__(self):
return self.question_text
class Choice(models.Model):
# ...
def __str__(self):
return self.choice_text
--In addition to the convenience of displaying in the shell, it is used as an object representation of django's auto-generated admin object. --So it's important to add the __str __ () method to your model
polls/models.py
from django.db import models
from django.utils import timezone
class Question(models.Model):
# ...
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
python manage.py createsuperuser
from django.contrib import admin
from .models import Question
admin.site.register(Question)
Recommended Posts