[英]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.