def dbDemo(request): #添加数据 test1 = Test(name='w3cschool.cc') test1.save() #通过objects这个模型管理器的all() 获得所有数据行,相当于SQL中的SELECT * FROM list = Test.objects.all() print list #filter 相当于SQL中的WHERE,可设置条件过滤结果 response0 = Test.objects.filter(id=1) print response0 #获取单个对象 response1 = Test.objects.get(id=1) print response1 return HttpResponse("<p>数据库操作成功!</p>")
class CreateModelActionView(TemplateResponseMixin, View): template_name = 'create_modelaction.html' def create(self, *args, **kwargs): self.object = Test(name='test', force_time=datetime.datetime.now().time()) self.object.save() return self.object def get(self, request, *args, **kwargs): self.create(*args, **kwargs) self.request = request return self.render_to_response({}) def post(self, request, *args, **kwargs): self.create(*args, **kwargs) self.request = request return self.render_to_response({})
def create(self, *args, **kwargs): self.object = Test(name='test', force_time=datetime.datetime.now().time()) self.object.save() return self.object