建立虛擬環境

1
2
3
4
5
python3.11 -m  venv venv 
#python3.9 -m venv venv

# Install package from requirements.txt
pip install -r requirements.txt

命名規範

參考PEP 8代碼規範

Django Mock

當後端工程師定義好api schema後,需要再花時間把商業邏輯進行實作,這段時間前端工程師只能等待,為了讓前端工程師可以先進行開發,可以先使用mock的方式來模擬後端api的回傳結果,這樣前端工程師就可以先進行開發,等到後端工程師完成實作後,再將mock的程式碼關閉,改成呼叫後端api。

範例參考

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# pip install pip install django-mock-queries


# Model
class Account(models.Model):
class Meta:
verbose_name = 'Account'
verbose_name_plural = 'Account'

name = models.CharField(verbose_name='Name',
max_length=255, )

def __str__(self):
return self.name

@classmethod
def get_mock_qs(cls):
qs = MockSet(
MockModel(id=1, name="kevin"),
MockModel(id=2, name="David"),
)
return qs

# View
class MockAccountViewSet(ViewSetUtils,
FilterMixin,
viewsets.GenericViewSet):
@action(methods=['GET'], detail=False, url_path='example')
def example(self, request, *args, **kwargs):
use_mock = request.GET.get('use_mock', False)
queryset = Account.get_mock_qs() if use_mock else Account.objects.all()

serializer = AccountItemOut(
queryset.first(),
many=False,
context={'request': request},
)
return self.response(data=serializer.data)

標記為廢棄函數

1
2
3
4
5
6
7
8
9
10
11
# Mark as deprecated function
import warnings


def func_a(value: str) -> str:
"""
deprecated func_a at 2023-02-28
:return: value
"""
warnings.warn("此方法已廢棄,請改用func_b", DeprecationWarning)
return value
1
2
3
4
反向代理, Reverse Proxy, re-ver-se pro-xy
PEP 8, Python Enhancement Proposals 8, pep 8
增強, Enhancement, en-han-ce-ment
建議, Proposals, pro-pos-als