• Django-manage.py shell命令


    1、进入shell模式

    python3 manage.py shell
    

     2、基本数据访问

    from loginApp.models import Event,Guest   #导入loginAPP应用下的models.py中的event和guest表
    
    Event.objects.all()  #获取event表中的所有对象
    
    Guest.objects.all()

     2、插入数据

    >>> e1 = Event(id=2,name='aa的pro发布会',limit=10,status=True,address='山东泰山',start_time=datetime(2021,2,15,0,0,0))
    >>> e1.save()
    
    /Users/bjhl/stone-web/lib/python3.6/site-packages/django/db/models/fields/__init__.py:1447: RuntimeWarning: DateTimeField Event.start_time received a naive datetime (2021-02-15 00:00:00) while time zone support is active.
      RuntimeWarning)    UTC有兴趣可查,可以在USE_TZ = False 关闭
    
    >>>  quit()
    再次进入shell模式查看,才能看到成功报错效果
    

      

    >>> from datetime import datetime
    >>> Event.objects.create(id=3,name='bb的pro发布会',limit=30,status=True,address='Asia',start_time=datetime(2021,2,15,0,0,59))   #创建并保存
    
    /Users/bjhl/stone-web/lib/python3.6/site-packages/django/db/models/fields/__init__.py:1447: RuntimeWarning: DateTimeField Event.start_time received a naive datetime (2021-02-15 00:00:59) while time zone support is active.
      RuntimeWarning)
    <Event: bb的pro发布会>
    
    >>> Guest.objects.create(realname="juju",phone=16812341234,email='andy@mail.com',sign=False,event_id=3)
    <Guest: juju>
    >>>

    3、查询数据(table.objects.get精确匹配)

    >>> Event.objects.get(name="aa的pro发布会")
    <Event: aa的pro发布会>
    >>> e1=Event.objects.get(name="aa的pro发布会")
    >>> e1.email
    Traceback (most recent call last):
      File "<console>", line 1, in <module>
    AttributeError: 'Event' object has no attribute 'email'
    >>> e1.address
    '山东泰山'
    >>> e1.start_time
    datetime.datetime(2021, 2, 15, 0, 0, tzinfo=<UTC>)
    >>> e1.limit
    10
    

    4、 模糊匹配

    >>> Guest.objects.select_for_update().filter(phone='15088132356').update(realname='hjahah')
    1
    >>> Guest.objects.select_for_update().filter(phone='15088132356')
    <QuerySet [<Guest: hjahah>]>
    

      

    >>> e2 =Event.objects.filter(name__contains="发布会")   #name__contains是双下划线,匹配到就返回一个对象列表。匹配不到,直接返回[]
    >>> e2 <QuerySet [<Event: 菲菲的新品发布会>, <Event: aa的pro发布会>, <Event: bb的pro发布会>]> >>>

    5、删除数据

    >>> Event.objects.get(name="bb的pro发布会")
    <Event: bb的pro发布会>
    >>> e3 =Event.objects.get(name="bb的pro发布会")
    >>> e3
    <Event: bb的pro发布会>
    >>> e3.delete()
    (2, {'loginApp.Guest': 1, 'loginApp.Event': 1})
    >>> e3 =Event.objects.get(name="bb的pro发布会")
    Traceback (most recent call last):
      File "<console>", line 1, in <module>
      File "/Users/bjhl/stone-web/lib/python3.6/site-packages/django/db/models/manager.py", line 85, in manager_method
        return getattr(self.get_queryset(), name)(*args, **kwargs)
      File "/Users/bjhl/stone-web/lib/python3.6/site-packages/django/db/models/query.py", line 379, in get
        self.model._meta.object_name
    loginApp.models.DoesNotExist: Event matching query does not exist.
    >>> e2 =Event.objects.filter(name__contains="发布会")
    >>> e2
    <QuerySet [<Event: 菲菲的新品发布会>, <Event: aa的pro发布会>]>
    

    6、更新数据

    >>> Guest.objects.select_for_update().filter(phone='15088132356').update(realname='hjahah')
    1
    >>> Guest.objects.select_for_update().filter(phone='15088132356')
    <QuerySet [<Guest: hjahah>]>  
    你要相信 一切都会好起来
  • 相关阅读:
    串学习笔记
    C深度剖析学习笔记
    记英语单词ag
    树学习笔记
    如何做好项目总结
    易学队第二次团队会议
    易学队第四次团队会议
    易学队第五次团队会议
    易学队第一次团队会议
    易学队第三次团队会议
  • 原文地址:https://www.cnblogs.com/feifei-cyj/p/14404240.html
Copyright © 2020-2023  润新知