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() #删除

  • 相关阅读:
    Contest
    【HDU2224】The shortest path(双调欧几里得dp)
    UVa400.Unix ls
    一些脚本
    省赛总结
    一些事
    一些笔记
    CSU 1526: Beam me out!
    以后可能用到的命令
    Codeforces Round #297 (Div. 2)
  • 原文地址:https://www.cnblogs.com/xiaoyichong/p/10921500.html
Copyright © 2011-2022 走看看