程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Django database and module models (4)

編輯:Python

In the previous section, we added an application to the project , Now add a database to it to interact with the database .

Django By default, there is a lightweight database called SQLite, When we want to change other databases , You need to bind the database , How to bind ? First, open the project's settings.py, Find the inside DATABASES, The inside engine、name、password It is OK to change the data information to be connected . This is the basic method of binding database and project .

 

Models Let's create the model :

The model mentioned above is actually the layout of the database , That is to design those databases , So what models are there . Let's go over it again :Models The classes inside are tables , Properties are fields ( Name ), An object of a class is a row of a table . Every one created Models All are Django.db.models.Model Subclasses of , That is to say, every model All inherited from models.Model, And every attribute ( Field ) All are Field Real series of . as follows :

from django.db import models
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')

The above is how to use the model database and model in the project . It seems quite simple ~~~ It doesn't seem difficult ~~~

Here we will have a database and model , With the model , How do we use this model ? That is, we use the tables in the database , How to add, delete, modify and query this table ?

open pycharm, Enter command line mode , Import the module of the project , like this :

>>> from polls.models import Choice, Question # Import the model classes we just wrote.
# No questions are in the system yet.
>>> Question.objects.all()
<QuerySet []>
# Create a new Question.
# Support for time zones is enabled in the default settings file, so
# Django expects a datetime with tzinfo for pub_date. Use timezone.now()
# instead of datetime.datetime.now() and it will do the right thing.
>>> from django.utils import timezone
>>> q = Question(question_text="What's new?", pub_date=timezone.now())
# Save the object into the database. You have to call save() explicitly.
>>> q.save()
# Now it has an ID.
>>> q.id
1
# Access model field values via Python attributes.
>>> q.question_text
"What's new?"
>>> q.pub_date
datetime.datetime(2012, 2, 26, 13, 0, 0, 775217, tzinfo=<UTC>)
# Change values by changing the attributes, then calling save().
>>> q.question_text = "What's up?"
>>> q.save()
# objects.all() displays all the questions in the database.
>>> Question.objects.all()
<QuerySet [<Question: Question object (1)>]>

The above is the operation of the data in the table . wait a minute , Here is another important point : When I want to print an object , This is the address of the printed object : It is not the specific data of the object we want , How to deal with this problem ? We were right models When the design , Add one more inside __str__() The method is ok . like this :

class Question(models.Model):
# ...
def __str__(self):
return self.question_text

When we print objects again , Is to print out the data of specific objects .

 


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved