# Showing models fields in admin page from another model

**URL:** <https://forum.djangoproject.com/t/showing-models-fields-in-admin-page-from-another-model/6805>\
**Category:** Using Django\
**Created:** [February 27, 2021, 10:39am UTC](https://forum.djangoproject.com/t/showing-models-fields-in-admin-page-from-another-model/6805 "2021-02-27T10:39:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thegritche](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/thegritche/32/2956_2.png) [@Thegritche](https://forum.djangoproject.com/u/Thegritche)\
**Post date:** [February 27, 2021, 10:39am UTC](https://forum.djangoproject.com/t/showing-models-fields-in-admin-page-from-another-model/6805/1 "2021-02-27T10:39:46Z")

</div>

Hello  
I’m learning Django for motnh now with a smal app I build.  
Now I try to display some linked fields from a model to another one in my admin page.  
I don’t understand if it’s possible and how

**My models** are ModelB and ModelA like this :

```
class ModelB(models.Model):
  name = models.CharField(max_length=50)
  prix=models.CharField(max_length=50)
  long=models.IntegerField()
  larg=models.IntegerField()

  def __str__ (self):
    return self.full_name
   

class ModelA(models.Model):
  code = models.CharField(max_length=50, unique=True, help_text="Code")
  foreignkey = models.ForeignKey(ModelB,on_delete=models.CASCADE)

```

**My admin.py**  
`@admin.register(ModelB)`

```
class ModelB(admin.ModelAdmin):
  list_display = ('name','prix','long','larg') 
  # yes it works

class ModelAAdmin(admin.ModelAdmin):
  list_display = ('code', 'foreignkey',)
  #list_display = ('code', 'foreignkey','my prix from Model B', 'my long from ModelB'....)
admin.site.register(ModelA, ModelAAdmin)

```

So now I want to display in the ModelA admin page : for each item in my ModelA database ,all fields taken from ModelB (prix,long,larg)

How do I do ?  
Thank for your explanations  
Patrice

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 27, 2021, 12:22pm UTC](https://forum.djangoproject.com/t/showing-models-fields-in-admin-page-from-another-model/6805/2 "2021-02-27T12:22:51Z")

</div>

You should be able to access the foreign fields using the “dot notation” just like you would reference those fields anywhere else:

> list\_display=(‘code’, ‘foreignkey.prix’, ‘foreignkey.long’, ‘foreignkey.larg’)

---

<div class="post-metadata">

**Author:** ![Thegritche](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/thegritche/32/2956_2.png) [@Thegritche](https://forum.djangoproject.com/u/Thegritche)\
**Post date:** [February 27, 2021, 3:40pm UTC](https://forum.djangoproject.com/t/showing-models-fields-in-admin-page-from-another-model/6805/3 "2021-02-27T15:40:21Z")

</div>

Hello Ken thanks for your reply , I try your solutions but the django server show an error

`<class 'blog.admin.ModelAAdmin'>: (admin.E108) The value of 'list_display[2]' refers to 'foreignkey.prix', which is not a callable, an attribute of 'ModelAAdmin', or an attribute or method on 'blog.ModelA'.`

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 27, 2021, 3:53pm UTC](https://forum.djangoproject.com/t/showing-models-fields-in-admin-page-from-another-model/6805/4 "2021-02-27T15:53:55Z")

</div>

Well, that’s what I get for relying upon my memory. The [list\_display docs](https://docs.djangoproject.com/en/3.1/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_display) do show that you can do this by creating functions in your admin class to return these values.

You could add functions like this in ModelAAdmin (one function for each value):

```auto
def fk_prix(self, obj):
    return obj.foreignkey.prix

```

and then `list_display = ('code', 'fk_prix', ...)`

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 27, 2021, 3:54pm UTC](https://forum.djangoproject.com/t/showing-models-fields-in-admin-page-from-another-model/6805/5 "2021-02-27T15:54:54Z")

</div>

Also, if this is something that you would want to do in multiple locations, you could also create functions like this in the Model class as well. (Again, see the docs for examples.)

---

<div class="post-metadata">

**Author:** ![Thegritche](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/thegritche/32/2956_2.png) [@Thegritche](https://forum.djangoproject.com/u/Thegritche)\
**Post date:** [February 27, 2021, 5:53pm UTC](https://forum.djangoproject.com/t/showing-models-fields-in-admin-page-from-another-model/6805/6 "2021-02-27T17:53:26Z")

</div>

Yes it work’s !! and today I understand something more in the Django’s logic

I had read the page you mention (many times) but not understood how I can use it in my propre models but now yes  
Thanks for your help and explanations
