Import attributes from models classes from another app

Asked

Viewed 503 times

0

Good afternoon guys, I’m good a doubt! I am using 2 Apps in Django admin

App1 = Registrations

App2 = Controlling Fellows

I imported into "Stock Changers" (App2) the app1 model, the "Person" class in the "Stock Changer" class, but it only takes the "Name" attribute and can only list the name in the Display_list in the app2 admin.py.

Can I list the other attributes of the "Person" class, such as address or data_birth in the display_list in my App2(Controlbolsistas)? and how I do it?

Anyone who can help me, I thank you!

registers/models.py

class Pessoa(models.Model):


    CPF = models.CharField(primary_key = True, max_length = 11)
    Nome = models.CharField(max_length= 45)
    Data_nascimento = models.DateField()
    Endereço = models.CharField(max_length=45)

    def __str__(self):
        return "%s" % (self.Nome)

controledfellows/models.py

from django.db import models
from ..cadastros.models import Pessoa

  class Bolsista(models.Model):


     pessoa = models.ForeignKey("cadastros.Pessoa")  


     def __unicode__(self):
         return 'Bolsista:' + self.pessoa

controledebolsistas/admin.py

from django.contrib import admin
from gestaobolsistas.controledebolsistas.models import Bolsista

    class BolsistaAdmin(admin.ModelAdmin):

        model=Bolsista
        list_display = ['pessoa']
        search_fields = ['pessoa']
    admin.site.register(Bolsista, BolsistaAdmin)

1 answer

0


A very simple way for you to do this is by defining a method in your BolsistaAdmin that returns this information. So it could be more or less like this:

from django.contrib import admin
from gestaobolsistas.controledebolsistas.models import Bolsista

class BolsistaAdmin(admin.ModelAdmin):

    model = Bolsista
    list_display = ('pessoa', 'get_pessoa_cpf',)
    search_fields = ('pessoa',)

    def get_pessoa_cpf(self, obj):
        return obj.pessoa.CPF
    get_pessoa_cpf.short_description = 'CPF'

admin.site.register(Bolsista, BolsistaAdmin)
  • Thanks friend, solved my problem here! Thank you very much, hug

Browser other questions tagged

You are not signed in. Login or sign up in order to post.