0

I'm programming an app for the edXproyect using Django and I need to retrieve some information from auth_user table and I don't know how to do it. I have this:

models.py

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)

admin.py

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)

But I would like to display something like:

list_display = ['id', 'usuario', 'full_name', 'recurso']

So I have to retrieve the id and full_name from the auth_table with the username field. I have tried adding classmethods in Recomendador like:

def get_id(self):
    return self.usuario.id

and now I want to call them from the RecomendacionAdmin class so I can list the id and full_name fields too but I don't know how to do it. I have googled it but I only get exceptions and errors with the solutions I find. Can anyone help me with this? I'm new to python...

László Papp
  • 51,870
  • 39
  • 111
  • 135

1 Answers1

0

Having a bit of trouble being 100% sure what you are looking for due to the source code not being English, but if you are looking to obtain data about Recomendacion inside of admin.py, since you are already importing Recomendacion, you should just be able to do the following:

recomendacion = Recomendacion.objects.get(username=desired_username)

That will give you the object who's username is whatever you are looking for, and then you can just access the id and full_name fields.

dursk
  • 4,435
  • 2
  • 19
  • 30