0

In Django's document Django Document

It has following code.

from django.contrib import admin

class AuthorAdmin(admin.ModelAdmin):
    fields = ('name', 'title', 'view_birth_date')

    def view_birth_date(self, obj ):
        return obj.birth_date

    view_birth_date.empty_value_display = '???'

I don't understand in the custom method view_birth_date(self, obj ) where this obj parameter came from?

Note in the last line, it called this function,

view_birth_date.empty_value_display = '???'

but did not pass any parameter for obj. I don't understand where how obj got a value.

Thanks!

1 Answer 1

2

I don't understand in the custom method view_birth_date(self, obj) where this obj parameter came from?

This method is called by Django's machinery and obj is passed also by it. It's just a predefined interface.

view_birth_date.empty_value_display = '???' but did not pass any parameter for obj. I don't understand where how obj got a value.

This line has nothing to do with your question. Again, according to the interface, Django when looking at your method (function) looks for empty_value_display attribute to find out what value the developer expects to see when the current value is empty.

Yes, looks weird but it's just how Django works, how its creators made it work. They chose this interface -- you just have to use the docs to find out the interface.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! I google around and could not find related material. Could you point some link for me understand this concept?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.