zoukankan      html  css  js  c++  java
  • [Django] Creating an app, models and database

    To add a new app, first cd to the project.

    Then run:

    python manage.py startapp scrumboard

    After that a new folder call 'scrumboard' will be created in you applicaiton folder.

    Now cd to scrumboard folder and open models.py:

    from django.db import models
    from django.utils.encoding import python_2_unicode_compatible
    
    @python_2_unicode_compatible
    class List(models.Model):
        name = models.CharField(max_length=50)
    
        def __str__(self):
            return "List {}".format(self.name)
    
    
    @python_2_unicode_compatible
    class Card(models.Model):
        title = models.CharField(max_length=100)
        description = models.TextField(blank=True)
    
        def __str__(self):
            return "Card {}".format(self.title)

    Add a database:

    python manage.py makemigrations

    It check whether there is anything changed in my models.py file.

    And you can see there is a new file generated inside 'migrations folder' called 0001_initial.py.

    Now let's do migrations:

    python manage.py migrate

    If we did any changes in models.py, we need to run mirgration again:

    from django.db import models
    from django.utils.encoding import python_2_unicode_compatible
    
    @python_2_unicode_compatible
    class List(models.Model):
        name = models.CharField(max_length=50)
    
        def __str__(self):
            return "List {}".format(self.name)
    
    
    @python_2_unicode_compatible
    class Card(models.Model):
        title = models.CharField(max_length=100)
        description = models.TextField(blank=True)
        list = models.ForeignKey(List, related_name="cards")
        story_points = models.IntegerField(null=True, blank=True)
        business_value = models.IntegerField(null=True, blank=True)
    
        def __str__(self):
            return "Card {}".format(self.title)

    More docs for Model: https://docs.djangoproject.com/en/1.9/topics/db/models/

  • 相关阅读:
    7617:输出前k大的数
    2991:2011
    7620:区间合并
    1688 求逆序对
    Magento How To Display Product Custom Option On list.phtml
    大二实习使用的技术汇总(中)
    codility上的问题 (22)
    poj 3321
    使用ViewPager实现左右“无限”滑动的万年历
    数论练习专题
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6507695.html
Copyright © 2011-2022 走看看