....
@admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
def method(self, request, queryset):
#获取外键关联模型 f类型为<class 'django.db.models.fields.related.ForeignKey'>
relations = [f.related_model.__name__ for f in self.model._meta.get_fields() if (f.many_to_one)]
# self.model. 获取admin注册的模型 related_model获取关联的模型 __name__模型名
......
type(f.related_model) <class 'django.db.models.base.ModelBase'> type(f.related_model.__name__) <class 'str'> f.related_model.__name__ 'BasisPay' f.related_model <class 'c4escalculation.models.BasisPay'> 标签:__,name,admin,模型,related,外键,model From: https://www.cnblogs.com/ldx-wsj/p/17001816.html