标签:带来 checkbox check 简单 workbench img 定义类 try user
pip install mysql-python
create databases test2 charset=utf8
DATABASES = {
‘default‘: {
‘ENGINE‘: ‘django.db.backends.mysql‘,
‘NAME‘: ‘test2‘,
‘USER‘: ‘用户名‘,
‘PASSWORD‘: ‘密码‘,
‘HOST‘: ‘数据库服务器ip,本地可以使用localhost‘,
‘PORT‘: ‘端口,默认为3306‘,
}
}
python manage.py inspectdb > booktest/models.py
bookinfo.heroinfo_set
heroinfo.bookinfo
heroinfo.book_id
<app_name>_<model_name>
class BookInfo(models.Model):
...
class Meta():
ordering = [‘id‘]
class BookInfo(models.Model):
...
class Meta():
ordering = [‘-id‘]
class BookInfo(models.Model):
btitle = models.CharField(max_length=20)
bpub_date = models.DateTimeField()
bread = models.IntegerField(default=0)
bcommet = models.IntegerField(default=0)
isDelete = models.BooleanField(default=False)
class HeroInfo(models.Model):
hname = models.CharField(max_length=20)
hgender = models.BooleanField(default=True)
isDelete = models.BooleanField(default=False)
hcontent = models.CharField(max_length=100)
hbook = models.ForeignKey(‘BookInfo‘)
insert into booktest_bookinfo(btitle,bpub_date,bread,bcommet,isDelete) values
(‘射雕英雄传‘,‘1980-5-1‘,12,34,0),
(‘天龙八部‘,‘1986-7-24‘,36,40,0),
(‘笑傲江湖‘,‘1995-12-24‘,20,80,0),
(‘雪山飞狐‘,‘1987-11-11‘,58,24,0)
insert into booktest_heroinfo(hname,hgender,hbook_id,hcontent,isDelete) values
(‘郭靖‘,1,1,‘降龙十八掌‘,0),
(‘黄蓉‘,0,1,‘打狗棍法‘,0),
(‘黄药师‘,1,1,‘弹指神通‘,0),
(‘欧阳锋‘,1,1,‘蛤蟆功‘,0),
(‘梅超风‘,0,1,‘九阴白骨爪‘,0),
(‘乔峰‘,1,2,‘降龙十八掌‘,0),
(‘段誉‘,1,2,‘六脉神剑‘,0),
(‘虚竹‘,1,2,‘天山六阳掌‘,0),
(‘王语嫣‘,0,2,‘神仙姐姐‘,0),
(‘令狐冲‘,1,3,‘独孤九剑‘,0),
(‘任盈盈‘,0,3,‘弹琴‘,0),
(‘岳不群‘,1,3,‘华山剑法‘,0),
(‘东方不败‘,0,3,‘葵花宝典‘,0),
(‘胡斐‘,1,4,‘胡家刀法‘,0),
(‘苗若兰‘,0,4,‘黄衣‘,0),
(‘程灵素‘,0,4,‘医术‘,0),
(‘袁紫衣‘,0,4,‘六合拳‘,0)
class BookInfo(models.Model):
...
books = models.Manager()
class BookInfoManager(models.Manager):
def get_queryset(self):
return super(BookInfoManager, self).get_queryset().filter(isDelete=False)
class BookInfo(models.Model):
...
books = BookInfoManager()
class BookInfo(models.Model):
...
@classmethod
def create(cls, title, pub_date):
book = cls(btitle=title, bpub_date=pub_date)
book.bread=0
book.bcommet=0
book.isDelete = False
return book
引入时间包:from datetime import *
调用:book=BookInfo.create("hello",datetime(1980,10,11));
保存:book.save()
class BookInfoManager(models.Manager):
def create_book(self, title, pub_date):
book = self.model()
book.btitle = title
book.bpub_date = pub_date
book.bread=0
book.bcommet=0
book.isDelete = False
return book
class BookInfo(models.Model):
...
books = BookInfoManager()
调用:book=BookInfo.books.create_book("abc",datetime(1980,1,1))
保存:book.save()
class BookInfoManager(models.Manager):
def create_book(self, title, pub_date):
book = self.create(btitle = title,bpub_date = pub_date,bread=0,bcommet=0,isDelete = False)
return book
class BookInfo(models.Model):
...
books = BookInfoManager()
调用:book=Book.books.create_book("abc",datetime(1980,1,1))
查看:book.pk
filter(键1=值1,键2=值2)
等价于
filter(键1=值1).filter(键2=值2)
print([e.title for e in Entry.objects.all()])
print([e.title for e in Entry.objects.all()])
querylist=Entry.objects.all()
print([e.title for e in querylist])
print([e.title for e in querylist])
filter(isDelete=False)
exclude(btitle__contains=‘传‘)
exclude(btitle__endswith=‘传‘)
filter(btitle__isnull=False)
filter(pk__in=[1, 2, 3, 4, 5])
filter(id__gt=3)
filter(bpub_date__year=1980)
filter(bpub_date__gt=date(1980, 12, 31))
filter(heroinfo_ _hcontent_ _contains=‘八‘)
filter(pk__lt=6)
from django.db.models import Max
maxDate = list.aggregate(Max(‘bpub_date‘))
count = list.count()
list.filter(bread__gte=F(‘bcommet‘))
list.filter(bread__gte=F(‘bcommet‘) * 2)
list.filter(isDelete=F(‘heroinfo__isDelete‘))
list.filter(bpub_date__lt=F(‘bpub_date‘) + timedelta(days=1))
from django.db.models import Q
list.filter(Q(pk_ _lt=6))
list.filter(pk_ _lt=6).filter(bcommet_ _gt=10)
list.filter(Q(pk_ _lt=6) | Q(bcommet_ _gt=10))
list.filter(~Q(pk__lt=6))
class AreaInfo(models.Model):
atitle = models.CharField(max_length=20)
aParent = models.ForeignKey(‘self‘, null=True, blank=True)
上级对象:area.aParent
下级对象:area.areainfo_set.all()
from models import AreaInfo
def area(request):
area = AreaInfo.objects.get(pk=130100)
return render(request, ‘booktest/area.html‘, {‘area‘: area})
<!DOCTYPE html>
<html>
<head>
<title>地区</title>
</head>
<body>
当前地区:{{area.atitle}}
<hr/>
上级地区:{{area.aParent.atitle}}
<hr/>
下级地区:
<ul>
{ %for a in area.areainfo_set.all%}
<li>{{a.atitle}}</li>
{ %endfor%}
</ul>
</body>
</html>
urlpatterns = [
url(r‘^area/$‘, views.area, name=‘area‘)
]
标签:带来 checkbox check 简单 workbench img 定义类 try user
原文地址:http://www.cnblogs.com/alamZ/p/7231137.html