from django.db import models
class Question(models.Model):
text = models.CharField(max_length=200)
pub_date = models.DateTimeField('published_date')
def __unicode__(self):
return self.text
先在 settings.py
中进行设置:
TIME_ZONE = 'Asia/Shanghai'
LANGUAGE_CODE = 'zh-CN'
然后创建一些数据:
>>> Question.objects.create(
text='what are you doing',
pub_date=datetime.datetime(2015, 11, 7)
)
>>> Question.objects.create(
text='what is wrong with you',
pub_date=datetime.datetime(2014, 11, 7)
)
>>> Question.objects.create(
text='who are you',
pub_date=datetime.datetime(2015, 10, 7)
)
>>> Question.objects.create(
text='who am i',
pub_date=datetime.datetime(2014, 10, 7)
)
>>> Question.objects.all()
[<Question: what are you doing>, <Question: what is wrong with you>, <Question: who are you>, <Question: who am i>]
AND
将多个 Q
对象作为非关键字参数或使用 &
联结即可实现 AND
查询:
>>> from django.db.models import Q
# Q(...)