Django


1452 浏览 5 years, 7 months

4 模型 Model

版权声明: 转载请注明出处 http://www.codingsoho.com/
from django.db import models
class Link(models.Model): 
url = models.URLField(unique=True)

Notice that Django automatically added an id field to the table

This field is the primary key of the table and can be used to identify links and connect them to bookmarks.

>>> link1 = Link(url='http://www.packtpub.com/')
>>> link1.save()
>>> link2 = Link(url='http://www.example.com/')
>>> link2.save()

List all

>>> links = Link.objects.all()

get an object by ID

>>> Link.objects.get(id=1)

delete a object

>>> link2.delete()

managers

Django:model类的objects属性

null=True是针对数据库的,而blank=True是针对validation的

获取Django model中字段名,字段的verbose_name,字段类型 - ccorz - 博客园 https://www.cnblogs.com/ccorz/p/huo-quDjango-model-zhong-zi-duan-ming-zi-duan-deve.html