[Django] Creating an app, models and database
2023-09-14 09:00:52 时间
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/
相关文章
- Django(13)django时区问题
- django官方入门教程_DJango
- 【Django】在大型项目中的django的性能模型字段primary_key
- ORA-01122: database file string failed verification check ORACLE 报错 故障修复 远程处理
- ORA-01219: database not open: queries allowed on fixed tables/views only ORACLE 报错 故障修复 远程处理
- ORA-01670: new datafile string needed for standby database recovery ORACLE 报错 故障修复 远程处理
- ORA-15519: cannot access database link “string” because it is disabled ORACLE 报错 故障修复 远程处理
- ORA-16750: failed to activate logical standby database ORACLE 报错 故障修复 远程处理
- Oracle 视图 DATABASE_PROPERTIES 官方解释,作用,如何使用详细说明
- MySQL 5.7: A Jar of Database Solutions(mysql5.7jar)
- “Bin vs Oracle:The Battle for Database Supremacy”(binoracle)
- cloud database突破传统,选择云数据库:Oracle Cloud Database(naloracle)
- Dockerizing Redis for Scalable Database Deployment.(dockerredis)
- StepbyStep Guide to Creating a Field in Oracle Database(oracle创建字段)