N
N
Nick V2016-02-15 21:02:59
Django
Nick V, 2016-02-15 21:02:59

Can you please explain why [Django Admin @staticmethod] is happening?

There are three models:

models.py

####################################
class CommonInfo(models.Model):
    name = models.CharField(max_length=100,
                            unique=True,
                            null=True,
                            verbose_name='Имя')
    def __str__(self):
        return self.name

    class Meta:
        abstract = True
####################################
class FacultyModel(CommonInfo):
    pass

    class Meta(CommonInfo.Meta):
        verbose_name_plural = 'Факультеты'
        verbose_name = 'Факультет'
        ordering = ('created_at',)
####################################
class DepartmentModel(CommonInfo):
    faculty = models.ForeignKey(FacultyModel,
                                verbose_name='Факультет')
    class Meta(CommonInfo.Meta):
        verbose_name_plural = 'Кафедры'
        verbose_name = 'Кафедра'
        ordering = ('faculty', 'created_at')
####################################
class GroupModel(CommonInfo):
    department = models.ForeignKey(DepartmentModel, 
                                   verbose_name='Кафедра')

    class Meta(CommonInfo.Meta):
        verbose_name_plural = 'Группы'
        verbose_name = 'Группа'
        ordering = ('department', 'created_at',)
####################################


And there is this code in the admin panel:
admin.py

@admin.register(GroupModel)
class GroupAdmin(CommonAdmin):
    list_display = ('name', 'department', 'get_faculty_name')

    def get_faculty_name(self, obj):
        return obj.department.faculty.name

    get_faculty_name.short_description = 'Факультет'
    get_faculty_name.admin_order_field = 'faculty__name'


The result of admin.py

As you can see, a field with a faculty has been added
d1baa32aff8d47aebd86611b29c9fc00.jpeg

Things are good. Everything fits. But here PyCharm suggests this thing:
Method 'get_faculty_name' may be 'static' less... (Ctrl+F1 Alt+T)
This inspection detects any methods which may safely be made static.

I make the method static:
admin.py

@admin.register(GroupModel)
class GroupAdmin(CommonAdmin):
    list_display = ('name', 'department', 'get_faculty_name')

    @staticmethod
    def get_faculty_name(obj):
        return obj.department.faculty.name

    get_faculty_name.short_description = 'Факультет'
    get_faculty_name.admin_order_field = 'faculty__name'


And it turns out this:
Such

010532cc364b49858407f5cffb6b4463.jpeg

Sobsno the question itself is this. Is it necessary to use this static method in this situation? If so, how can I return the name of the field? I understand that if a static method is used, then it picks up the name from the name of the method.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2016-02-16
@half-life

I won’t tell you the implementation details in django admin either, but why pycharm gives such a hint is very easy to understand - you don’t use self in your method, which means it suggests itself to make the method static.

D
Dmitry Voronkov, 2016-02-16
@DmitryVoronkov

@admin.register(GroupModel)
class GroupAdmin(CommonAdmin):
    list_display = ('name', 'department', 'department__faculty')

Maybe it will be easier?
In general, it's better not to listen to pycharm and look at the django documentation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question