初始Django專案並客製化設定檔 – Django Setup With Split Settings

Django Setup 專案初始化

假設我們想要建立一個django專案(1.9版本,linux為例),命名為mysite,並且不想要將特定的設定上傳,可以如何做:

clone你的倉庫(myrepo)至本地

建立虛擬環境

可以透過指令或IDE,我習慣會將環境命名為venv,然後放在專案目錄下

安裝django並開始一個專案

初始化django專案

別忘了把你安裝的套件加到mysite/requirements.txt

這時候你的tree應該長這樣

Read more “初始Django專案並客製化設定檔 – Django Setup With Split Settings”

發表留言

Django: Create an instance of Manager with a copy of a custom QuerySet

Sometimes we want to query by custom filter and different orders

And in this case will raise AttributeError because Queryset object can not call Manager method

Solution: as_manager()

More Django settings from document:

發表留言

Django Unit Test with Fixtures

Sometimes we need to write test with initial data

For this setting as example, we can load initial data by

And we need to do setup in test cases

發表留言

Django: Query from abstract model class

Abstract classes such as AbstractProduct which inherit from third party package model_utils.InheritanceManage, will not work with abstract attribute specified true:

For this reason, its subclass like ProductA will cause create issue when load fixtures data by running this command:

Which will not raise errors also not writing into database, see the same issue on stackoverflow

The solution here is provide post_save signal and execute save(), because loaddata command will not call pre_save() and save()

發表留言

Django: rest_framework

ModelSerializer

data

retrieve lookup

Override APIView

permission

HyperlinkedIdentityField

MethodField

    發表留言

    Django Staticfiles

    urls.py

    settings.py

    collectstatics: add internal statics to external(dev) statics

    Direcotry

     

    發表留言