[英]call @classmethod from another @classmethod of another class
[英]How can I call a classmethod from another class in python?
我正在使用Django为edXproyect编写应用程序,我需要从auth_user表中检索一些信息,但我不知道该怎么做。 我有这个:
from django.db import models
from django.contrib.auth.models import User
class Recomendacion(models.Model):
usuario = models.ForeignKey(User, db_column='username', primary_key=True)
recurso = models.CharField(max_length=255, db_column='next_module')
def __str__(self):
return u'%s' % (self.usuario.username)
from django.contrib import admin
from recomendador.models import Recomendacion
class RecomendacionAdmin(admin.ModelAdmin):
fields = ['usuario', 'recurso']
list_display = ['usuario', 'recurso']
admin.site.register(Recomendacion, RecomendacionAdmin)
但我想显示如下内容:
list_display = ['id', 'usuario', 'full_name', 'recurso']
因此,我必须使用username
auth_table
段从auth_table
检索id
和full_name
。 我试过在Recomendador中添加类方法,例如:
def get_id(self):
return self.usuario.id
现在我想从RecomendacionAdmin
类中调用它们,这样我也可以列出id
和full_name
字段,但是我不知道该怎么做。 我已经用谷歌搜索了,但是我找到的解决方案只有异常和错误。 谁能帮我这个? 我是python的新手...
由于源代码不是英语,因此在100%不确定要查找的内容时有些麻烦,但是如果您想在admin.py
获取有关Recomendacion
的admin.py
,因为您已经在导入Recomendacion
,则应该能够执行以下操作:
recomendacion = Recomendacion.objects.get(username=desired_username)
这将为您提供对象名,即您要查找的用户名,然后您就可以访问id
和full_name
字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.