Skip to content
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

def get_days_since_created(self, obj) 問題請教 #2

Open
ekils opened this issue Apr 27, 2018 · 1 comment
Open

def get_days_since_created(self, obj) 問題請教 #2

ekils opened this issue Apr 27, 2018 · 1 comment

Comments

@ekils
Copy link

ekils commented Apr 27, 2018

不好意思,python新手,這邊有個概念不是很懂:
class MusicSerializer(serializers.ModelSerializer):
days_since_created = serializers.SerializerMethodField()
def get_days_since_created(self, obj):
return (now() - obj.created).days

我的理解是class MusicSerializer 繼承了serializers.ModelSerializer ,然後他利用他底下的method get_days_since_created 去回傳一個值。
物件obj 則是呼叫了music。
不理解的地方:
這個def 裡面放了參數obj,但我沒看到其他地方有呼叫這個method,他是怎麼可以運行的?

@twtrubiks
Copy link
Owner

@ekils 用中斷點下去看會比較清楚,他是在 Serializer 的時候觸發的

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants