zoukankan      html  css  js  c++  java
  • django 的网站创建步骤(四)Shell的使用

    1.执行命令 : python manage.py shell 打开一个控制台

    2. 执行如下命令序列进行,并查看结果

    >>>from polls.models import Choice,Question

    >>>Question.objects.all()

    >>>from django.utils import timezone

    >>>q = Question(question_text =" What 's new ?",pub_date=timezone.now())

    >>>q.save()

    >>>q.id

    >>>q.question_text

    >>>q.pub_date

    >>>q.question_text = "What's up?"

    >>>q.save()

    >>>Question.objects.all()

    3.修改数据模型:

    再执行shell命令,并查看结果:

    >>> from polls.models import Choice,Quesiton

    >>>Question.objects.all()

    >>>Question.objects.filter(id=1) #相当于where id=1 

    >>>Question.objects.filter(question_text__startswith='What')

    >>>from django.utils import timezone

    >>>current_year = timezone.now().year

    >>>Question.objects.get(pub_date__year=current_year)

    >>>Question.objects.get(id=2)

    这个会报错是正常的,错误如下:

    Tranceback (most recent call last):

    ...

    DoesNotExist :Question matching query does not exist.

    >>>Question.objects.get(pk=1)  #pk其实就是id,pk的意思就是关键字

    >>>q = Question.objects.get(pk=1)

    >>>q.was_published_recently()  #返回结果是True

    >>>q = Question.objects.get(pk=1)

    >>> q.choice_set.all() #取关联的所有数据

    >>>q.choice_set.create(choice_text='Not much',votes=0)

    >>>q.choice_set.create(choice_text='The sky',votes=0)

    >>>c=q.choice_set.create(choice_text='Just hacking again',voites=0)

    >>>c.question

    >>>q.choice_set.all()

    >>>q.choice_set.count() #返回3

    >>>Choice.objects.filter(question__pub_date__year =current_year)

    >>>c=q.choice_set.filter(choice_text__startswith='Just hacking')

    >>>c.delete() #删除

  • 相关阅读:
    三行代码搞定微信登录集成
    iptables命令
    Linux(centos)系统各个目录的作用详解 推荐
    Linux下Apache服务的查看和启动
    Linux使用退格键时出现^H ^?解决方法
    小程序:最难点For的wx:key
    linux 通过wol远程开机【转】
    linux wake on lan功能通过ethtool配置【转】
    设计模式小议:state【转】
    TCP/IP详解学习笔记(4)-ICMP协议,ping和Traceroute【转】
  • 原文地址:https://www.cnblogs.com/xiaoyichong/p/10921500.html
Copyright © 2011-2022 走看看