0

I found the following code block in an open source python project and to be honest have never seen the usage before

class PartAdmin(admin.ModelAdmin):
    ordering = ('organization', 'number_class__code', 'number_item', 'number_variation')
    readonly_fields = ('get_full_part_number',)
    list_display = (
        'organization',
        'get_full_part_number',
    )
    raw_id_fields = ('number_class', 'primary_manufacturer_part',)
    inlines = [
        ManufacturerPartAdminInline,
    ]

    def get_full_part_number(self, obj):
        return obj.full_part_number()

    get_full_part_number.short_description = 'PartNumber'
    get_full_part_number.admin_order_field: str = 'number_class__part_number'

What are these last two lines doing? What exactly are you accessing from this callable object?

po.pe
  • 1,047
  • 1
  • 12
  • 27
  • That depends on the rest of the code—you can't see in this example who's accessing this but some other code can access the fields like `PartAdmin.get_full_part_number.short_description` and `PartAdmin.get_full_part_number.admin_order_field`. I don't want to assume intention but looking at the names it might have to do something with auto-documenting and/or reflection facilities of the project. – zwer Sep 02 '20 at 11:56
  • That seems really weird since, as far as I know, you shouldn't be able to access a member of a class from within the body itself anyway. Would you mind sharing which project exactly you got this from? – r0w Sep 02 '20 at 11:57
  • Indabom https://github.com/mpkasp/indabom/tree/master/indabom – po.pe Sep 02 '20 at 11:58
  • @zwer, but if `get_full_part_number` is a method, what am I actually accessing with this? What's the object type? – po.pe Sep 02 '20 at 12:00
  • It looks like Django extensions: (Here's a similar question)[https://stackoverflow.com/questions/7337681/what-method-attributes-are-used-in-django] – RufusVS Sep 02 '20 at 12:05
  • Everything is an object in Python. A function is a function object. If you define `def foo(): pass` you can do `print(type(foo))` and you'll get ``. Now try `foo.bar = 'baz'` and voila - it works. – Matthias Sep 02 '20 at 12:05
  • That means I can add new object attributes outside of the actual object, but I cannot use them from, in this case, within the method? What's a reason for this? Why not declaring `short_description` and `admin_order_field` as class members of `PartAdmin`? – po.pe Sep 02 '20 at 12:11
  • There's a metaclass you aren't seeing because it belongs to a parent class. Trace through the inheritance tree and you'll find it – Mad Physicist Sep 02 '20 at 12:14
  • @po.pe - But you can access it from within itself, just slightly convoluted: `self.__class__.get_full_part_number.short_description`. When such functions are _naked_ (i.e. not wrapped/decorators) adding extra variables to them are usually used from the outside (e.g. some auto-doc traversal script) so there isn't a convenient way to reference itself, but nothing technically stops you from adding any member to any object for whatever reason you deem necessary. – zwer Sep 02 '20 at 12:22

0 Answers0