Django Notes V: Writing models

Source: Internet
Author: User

Models in fact, there is nothing to say, is the ordinary Python class

Configuring database connections in Settings

DATABASES = {    'default': {        'ENGINE':'Django.db.backends.mysql',        'NAME':'MyDB',        'USER':'Root',        'PASSWORD':"',        'HOST':'127.0.0.1',        'PORT':'3306',    }}

Define your own models.

classUser (models. Model):classMeta:verbose_name= u'User'verbose_name_plural= u'User'db_table='User'user_name= Models. Charfield (verbose_name=u'User name', max_length=100) Pass_word= Models. Charfield (verbose_name=u'Password', max_length=100)    def __str(self):returnSelf.user_nameclassarticle (models. Model):classMeta:verbose_name= u'article'verbose_name_plural= u'article'db_table='article'title= Models. Charfield (verbose_name=u'title', max_length=200) Content= Models. TextField (verbose_name=u'content') Create_time= Models. Datetimefield (verbose_name=u'creation Time', auto_now_add=True) last_modified= Models. Datetimefield (verbose_name=u'Last Modified Time', auto_now=True)def __str__(self):returnSelf.titleclassComment (models. Model):classMeta:verbose_name= u'Reviews'verbose_name_plural= u'Reviews'db_table='Comment'content= Models. TextField (verbose_name=u'Comment Content') Article= Models. ForeignKey (article, verbose_name='article') User= Models. ForeignKey (User, Verbose_name=u'Comment Person')    def __str__(self):returnSELF.CONTENT[0:20]

Execute Migrate Build database table

Python manage.py Makemigrationspython manage.py Migrate

Python manage.py sqlmigrate app_name Migrate_file_name can display the generated SQL

Other details, as well as how to configure multi-operation multi-database, etc., directly refer to the official documents on the line.

There are mysqldb do not support Python3, Python3 please use Pymysql

Just put the following two sentences in your app's __init__.py.

Import pymysqlpymysql.install_as_mysqldb ()

Django Notes V: Writing models

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.