西维蜀黍

【Django】Django ORM - 文档整理

DB Doc

Index for DB: https://docs.djangoproject.com/en/2.2/topics/db/

API Doc

QuerySet API reference:https://docs.djangoproject.com/en/1.11/ref/models/querysets/

Performance and optimization - https://docs.djangoproject.com/en/1.11/topics/performance/

  ...


【Django】Django ORM - 性能优化

你可以使用 python manage.py shell 来让 ORM 执行查询以玩耍:

$ python manage.py shell
>>> from core import models
>>> a = models.User.objects[0:5]
...

如果你使用 MySQL,还可以结合General Log来实时查看 MySQL的 SQL 执行日志以进行验证(Visit 【MySQL】日志记录 for more details)。

最重要的原则: Work at the appropriate level

意思就是说要在对应的 level (M V C) 做对应的事。e.g. 如果计算 court, 在最低的数据库 level 里是最快的(如果只需要知道此记录是否存在的话,用 exists() 会更快)。 但要 注意 : queryset 是 lazy 的,所以有时候在 higher level (例如模板)里控制 queryset 是否真的执行,说不定会更高效。

下面这段代码很好的解释了不同 level 的意思:

# QuerySet operation on the database
# fast, because that's what databases are good at
my_bicycles.count()

# counting Python objects
# slower, because it requires a database query anyway, and processing
# of the Python objects
len(my_bicycles)

# Django template filter
# slower still, because it will have to count them in Python anyway,
# and because of template language overheads
\{\{ my_bicycles|length \}\}
  ...


【Django】Django ORM - 使 DB Schema 生效

当通过 Model 声明完表结构后,Django 可以自动将 Model 声明转换成DB Schema,进而自动写入 DB 中。

在>=1.7 的 Django 中,可以使用以下方法来生成 DB schema:

$ python manage.py makemigrations
$ python manage.py migrate

The makemigrations command looks at all your available models and creates migrations for whichever tables don’t already exist. migrate runs the migrations and creates tables in your database, as well as optionally providing much richer schema control.

在 1.7 之前的版本,使用:

$ python manage.py syncdb
  ...


【Django】使用 Template

在复杂的 Django Project 中,可以会存在多个 Django App,如果科学地管理我们的 HTML页面非常重要。

  ...


【MySQL】日志(Logs)

MySQL 日志文件有一下几种:

  • 错误日志(error logs):It contains information about errors that occur while the server is running (also server start and stop)
  • 一般日志(general logs):This is a general record of what mysqld is doing (connect, disconnect, queries)
  • 慢查询日志(slow query logs):Ιt consists of “slow” SQL statements (as indicated by its name).
  • 二进制日志(binlog)
  • 重做日志(Redo Logs)
  • 回滚日志(Undo Logs)
  • Relay Logs
  ...


【Python】枚举

  ...


【Python】is 和 ==

is和==都是对对象进行比较判断作用的,但对对象比较判断的内容并不相同。下面来看看具体区别在哪?

is比较的是两个对象的id值是否相等,也就是比较两个对象是否为同一个实例对象,是否指向同一个内存地址。

==比较的是两个对象的内容是否相等,默认会调用对象的__eq__()方法。

要理解Python中is和==的区别,首先要理解Python对象的三个要素:

要素 说明 获取方式
id 身份标识,基本就是内存地址,用来唯一标识一个对象 id(obj)
type 数据类型 type(obj)
value :—–:

is和==区别

标识 名称 判断方法
is 同一性运算符 id
== 比较运算符 value
  ...


【Django】Django 静态资源和 HTML 文件管理

Background

django.contrib.staticfiles

django.contrib.staticfiles是django1.3新增的一个app来帮助开发者管理静态文件(js,css等)。

在我使用的 Django 1.6.11版本中,默认已安装并加载了 staticfiles App,

INSTALLED_APPS = [
    # 'django.contrib.admin',
    # 'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'core',
]
  ...


【Architectural Pattern】前端框架中的 MVC、MVP 和 MVVM

1 原始的MVC (Model - View - Controller)

MVC (Model - View - Controller)架构是一种当前非常流行的架构模式(Architectural Pattern),被广泛使用于传统的桌面GUI(Graphical user interface)、Web应用和手机移动应用中。

  ...


【Distributed System】分布式 Session(Distributed Seesion)

Background

Session的作用?

Session 是客户端与服务器通讯会话跟踪技术,服务器与客户端保持整个通讯的会话基本信息。

客户端在第一次访问服务端的时候,服务端会响应一个sessionId并且将它存入到本地cookie中,在之后的访问会将cookie中的sessionId放入到请求头中去访问服务器,如果通过这个sessionid没有找到对应的数据那么服务器会创建一个新的sessionid并且响应给客户端。

分布式Session存在的问题?

假设第一次访问服务A生成一个sessionid并且存入cookie中,第二次却访问服务B客户端会在cookie中读取sessionid加入到请求头中,如果在服务B通过sessionid没有找到对应的数据那么它创建一个新的并且将sessionid返回给客户端,这样并不能共享我们的Session无法达到我们想要的目的。

  ...