Answer the question
In order to leave comments, you need to log in
Can you please explain why [Django Admin @staticmethod] is happening?
There are three models:
####################################
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',)
####################################
@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'
@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'
Answer the question
In order to leave comments, you need to log in
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.
@admin.register(GroupModel)
class GroupAdmin(CommonAdmin):
list_display = ('name', 'department', 'department__faculty')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question