Read Django

reference #

Run PG in container #

podman pod create --name pg -p 9876:80 -p 5432:5432
podman run --pod pg --name admin -d \
    -e 'PGADMIN_DEFAULT_EMAIL=postgres@myctl.space' -e 'PGADMIN_DEFAULT_PASSWORD=postgres' \
    docker.io/dpage/pgadmin4:latest
podman run --pod pg --name db -d \
    -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres \
    docker.io/library/postgres:latest

podman generate kube pg -f pg.yml
# 生成的文件中缺少了环境变量,可以手动添加
# Save the output of this file and use kubectl create -f to import
# it into Kubernetes.
#
# Created with podman-3.4.4
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: "2022-12-06T14:33:35Z"
  labels:
    app: pg
  name: pg
spec:
  containers:
  - args:
    - postgres
    image: docker.io/library/postgres:latest
    name: db
    ports:
    - containerPort: 80
      hostPort: 9876
    - containerPort: 5432
      hostPort: 5432
    env:
    - name: POSTGRES_USER
      value: "postgres"
    - name: POSTGRES_PASSWORD
      value: "postgres"
    resources: {}
    securityContext:
      capabilities:
        drop:
        - CAP_MKNOD
        - CAP_NET_RAW
        - CAP_AUDIT_WRITE
    volumeMounts:
    - mountPath: /var/lib/postgresql/data
      name: 40c074d419db893a9a41fe504f792eab4cd10f59cf3796ffa4423514811b92b0-pvc
  - image: docker.io/dpage/pgadmin4:latest
    name: admin
    resources: {}
    env:
    - name: PGADMIN_DEFAULT_EMAIL
      value: "admin@myctl.space"
    - name: PGADMIN_DEFAULT_PASSWORD
      value: "postgres"
    securityContext:
      capabilities:
        drop:
        - CAP_MKNOD
        - CAP_NET_RAW
        - CAP_AUDIT_WRITE
    volumeMounts:
    - mountPath: /var/lib/pgadmin
      name: f1f2d7230d6716504520f8a5f602a70ffea61ff571d13239c1552d53a46d9e5a-pvc
  restartPolicy: Never
  volumes:
  - name: 40c074d419db893a9a41fe504f792eab4cd10f59cf3796ffa4423514811b92b0-pvc
    persistentVolumeClaim:
      claimName: 40c074d419db893a9a41fe504f792eab4cd10f59cf3796ffa4423514811b92b0
  - name: f1f2d7230d6716504520f8a5f602a70ffea61ff571d13239c1552d53a46d9e5a-pvc
    persistentVolumeClaim:
      claimName: f1f2d7230d6716504520f8a5f602a70ffea61ff571d13239c1552d53a46d9e5a
status: {}

配置 #

#

RawQuerySet: Provide an iterator which converts the results of raw SQL queries into annotated model instances.

多数据库方案 #

https://docs.djangoproject.com/zh-hans/4.1/topics/db/multi-db/

DATABASE 上设置 default 是必须的,默认

DATABASES = {
    'default': {
        'NAME': 'postgres',
        'ENGINE': 'django.db.backends.postgresql',
        'HOST': '127.0.0.1',
        'PORT': '5432',
        'USER': 'postgres',
        'PASSWORD': 'postgres',
    },
    'lite': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

--database 适用 #

python manage.py migrate --database=lite

OneToOneField(ForeignKey)

ForeignKey(Foreign.Object)

  • models.fields.related.ForeignObject(RelatedField)

Abstraction of the ForeignKey relation to support multi-column relations.

  • models.fields.reverse_related.ForeignObjectRel(FieldCacheMixin)

Used by ForeignObject to store information about the relation.

WASI networking https://www.youtube.com/watch?v=353mpfL8r3s DjangoTricks https://www.djangotricks.com/tricks/LimXfEpqPwVB/ https://djangofeeds.com/

Powershell for Hyper-V #

get-vm

PG #

PGPASSWORD=cat /var/apps/database-password.secret pg_dump -Fc -h$HOST -p$PORT -U$USER $DB -f $FILE

~/.pg_service.conf PGSERVICE

~/.psqlrc

PGPASSWORD

Django deprecate timeline #

https://docs.djangoproject.com/en/dev/internals/deprecation/

migration 中更多的问题 #

–fake –fake-initial

Django ORM #

ForeignKey OneToOneField ManyToManyFiled

mypy #

Converting fx-private-relay to use mypy https://github.com/typeddjango/django-stubs

https://github.com/polyrabbit/hacker-news-digest

https://pypi.org/project/Wikipedia-API/ https://dumps.wikimedia.org/

https://github.com/meilisearch/instant-meilisearch/ https://tech.marksblogg.com/meilisearch-full-text-search.html

https://github.com/HackerNews/API https://hn.algolia.com/api

slicing #

class Inspector: def getitem(self, key): return key

a = Inspector() a[1] # => 1: int a[1, 2] # => (1, 2): tuple a[1, 2, 3] # => (1, 2, 3): tuple a[] # => SyntaxError

a[1:2:3] # => slice(1, 2, 3) assert a[1:2:3] == slice(1, 2, 3)

slice(stop) slice(start, stop[, step]) Create a slice object. This is used for extended slicing (e.g. a[0:10:2]).

[start:], [start::], [:stop] , [:stop:], [::step], [start:stop], [start:stop:], [start::step], [:stop:step], [start:stop:step]

index method

setitem and delitem are similar method with getitem .

dunder / magic methods #

https://docs.python.org/3/reference/datamodel.html#special-method-names https://rszalski.github.io/magicmethods/ https://martinheinz.dev/blog/87

Magic Method | When it gets invoked (example) | Explanation ——————————–+———————————–+————- new(cls [,…]) | instance = MyClass(arg1, arg2) | new is called on instance creation init(self [,…]) | instance = MyClass(arg1, arg2) | init is called on instance creation cmp(self, other) | self == other, self > other, etc. | Called for any comparison pos(self) | +self | Unary plus sign neg(self) | -self | Unary minus sign invert(self) | ~self | Bitwise inversion index(self) | x[self] | Conversion when object is used as index nonzero(self) | bool(self) | Boolean value of the object getattr(self, name) | self.name # name doesn’t exist | Accessing nonexistent attribute setattr(self, name, val) | self.name = val | Assigning to an attribute delattr(self, name) | del self.name | Deleting an attribute getattribute(self, name) | self.name | Accessing any attribute getitem(self, key) | self[key] | Accessing an item using an index setitem(self, key, val) | self[key] = val | Assigning to an item using an index delitem(self, key) | del self[key] | Deleting an item using an index iter(self) | for x in self | Iteration contains(self, value) | value in self, value not in self | Membership tests using in call(self [,…]) | self(args) | “Calling” an instance enter(self) | with self as x: | with statement context managers exit(self, exc, val, trace) | with self as x: | with statement context managers getstate(self) | pickle.dump(pkl_file, self) | Pickling setstate(self) | data = pickle.load(pkl_file) | Pickling

shebang with options #

coreutils 的特性

#!/usr/bin/env -S hurl --test

Django 环境 #

https://docs.djangoproject.com/en/4.1/topics/settings/ https://djangostars.com/blog/configuring-django-settings-best-practices/ https://iammahir.com/2022/07/11/configure-django-settings-for-multiple-environments/

SSH TUN/TAP tunneling #

https://goteleport.com/blog/ssh-tunneling-explained/ https://www.marcfargas.com/2008/07/ip-tunnel-over-ssh-with-tun/ https://en.wikibooks.org/wiki/OpenSSH/Cookbook/Proxies_and_Jump_Hosts#Passing_Through_a_Gateway_with_an_Ad_Hoc_VPN