-
Notifications
You must be signed in to change notification settings - Fork 93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
介紹一些 Model 與 Manager 的使用策略 #24
Labels
Comments
突然發現 class Region(models.Model):
# ...
def get_contained_remotes(self):
return Remote.objects.filter(
location__isnull=False,
location__contained=self.boundary,
)
def get_visible_remotes(self, user):
remotes = self.get_contained_remotes().get_viewable(user=user)
return remotes Well. |
直接繼承Manager這樣做應該也可以?
|
繼承 queryset 有個好處是串其他的條件比較方便。像這樣: Restaurant.objects.filter_fastfood().filter_cheap() 就一定要把至少把其中一個定義在 QuerySet。所以為了方便起見,這種東西通常都是定義在 QuerySet,然後再用 |
了解,沒想到可以這樣用。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fat model and mangers, forms when suitable, thin views, trivial templates.
節錄一些 Gitter 對話
壞範例:
好範例:
最主要的好處其實是會讓你的 view 很好讀
找到一個好像還不錯的例子
有個 project 需要找到使用者在一個範圍內能看到的所有 remote 物件
邏輯大概像這樣
我們把測試使用者權限的程式抽出來
然後把找 region 中 remotes 的程式抽出來
最後在 view 裡就變成這樣:
The text was updated successfully, but these errors were encountered: