Using the Django Development blog process to record the design of the database

Source: Internet
Author: User

1. Database Design2. Inserting test data3. Configuration related Issues

1. Database Design

The database has a simple three tables: article, Category, tag below is the code

1 #-*-coding:utf-8-*-2  from __future__ Importunicode_literals3 4  fromDjango.dbImportModels5 6 7 classarticle (models. Model):8Status_choices = (9('D','Draft'),Ten('P','Published'), One     ) A  -title = models. Charfield ('title', max_length=64, unique=True) -Content = models. TextField ('Body') theCreate_time = models. Datetimefield ('creation Time') -Last_modified_time = models. Datetimefield ('Modification Time') -Status = Models. Charfield ('article Status', Max_length=1, choices=status_choices) -Abstract = models. Charfield ('Summary', max_length=64, Blank=true, Null=true, help_text='Optional, if empty, the first 64 characters of the body will be extracted') +views = models. Positiveintegerfield ('Browse Times', default=0) -Likes = models. Positiveintegerfield ('To Click the number of likes', default=0) +topped = models. Booleanfield ('pinned', default=False) A  atCategory = Models. ForeignKey ('Category', verbose_name='category', Null=true, on_delete=models. Set_null) -tags = models. Manytomanyfield ('Tag', verbose_name='Label Collection', blank=True) -  -     def __unicode__(self): -         returnSelf.title -  in     classMeta: -ordering = ['-last_modified_time'] to  + classCategory (models. Model): -Name = models. Charfield ('class name', max_length=20) theCreate_time = models. Datetimefield ('creation Time', auto_now_add=True) *Last_modified_time = models. Datetimefield ('Modification Time', auto_now=True) $ Panax Notoginseng     def __unicode__(self): -         returnSelf.name the  + classTag (models. Model): AName = models. Charfield ('Label name', max_length=20) theCreate_time = models. Datetimefield ('creation Time', auto_now_add=True) +Last_modified_time = models. Datetimefield ('Modification Time', auto_now=True) -  $     def __unicode__(self): $         returnSelf.name

2. Inserting test data

At this time login admin background no data, so want to use to write a populate.py responsible for inserting test data The following is the code:

1 #-*-coding:utf-8-*-2 Importdatetime3 ImportOS4Os.environ.setdefault ('Django_settings_module','pureblog.settings')5 6 ImportDjango7 Django.setup ()8 9  fromApps.modelsImportarticle, Category, TagTen  One defpopulate (): Atags = [] -Cat_linux = Add_cat ('Linux') -Tag_ubantu = Add_tag ('Ubantu') theTag_redhat = Add_tag ('Redhat') - tags.append (Tag_ubantu) - tags.append (tag_redhat) -Add_article ('Title 1','P', Cat_linux, tags) +  - defadd_article (title, status, Cat, tags): +A = Article.objects.get_or_create (title=title, ACreate_time=Datetime.datetime.utcnow (), atLast_modified_time=Datetime.datetime.utcnow (), -status=status) [0] -A.content ='This is the test data .' -A.abstract = a.content[:64] -A.category =Cat -  in      forTaginchTags: - A.tags.add (TAG) to A.save () +     returna -  the defAdd_cat (name): *c = Category.objects.get_or_create (Name=name, create_time=Datetime.datetime.utcnow ()) [0] $     returnCPanax Notoginseng  - defAdd_tag (name): thet = Tag.objects.get_or_create (Name=name, create_time=Datetime.datetime.utcnow ()) [0] +     returnT A  the if __name__=='__main__': +Populate ()
3. Configuration related issues

1) because my database uses a MySQL database, you may encounter character encoding problems when inserting data, and the following link is a link to this issue:

Http://stackoverflow.com/questions/6065037/rails-mysql-encoding-issue-question-mysqlerror-illegal-mix-of-collations-l

Using the Django Development blog process to record the design of the database

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.