Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display all my model's fields in django admin?

Tags:

python

django

This code displays objects like this: Home Object(1) ,Home Object(2) but I want to display all the model fields in my django admin page.How can I do this?

I am very beginner in django and this is my first project.

models.py

class Home(models.Model):
    image=models.ImageField(upload_to='home')
    paragraph=models.TextField()
    created=models.DateTimeField(auto_now_add=True)

admin.py

admin.site.register(Home)
like image 806
dipak Avatar asked Oct 28 '25 17:10

dipak


1 Answers

If you want to change the display from Home Object(1) to something else, you can do this by defining a method in your model like this:

class Home(models.Model):
    image=models.ImageField(upload_to='home')
    paragraph=models.TextField()
    created=models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return "{}:{}..".format(self.id, self.paragraph[:10])

This will return object id with the first 10 characters in your paragraph on admin panel:

e.g: 1:Example par...

Once you click on that object, you will get object details.

If you want on panel, you can try like this in admin.py

from django.contrib import admin

class HomeAdmin(admin.ModelAdmin):
    list_display = ('image', 'paragraph','created',)

admin.site.register(Home,HomeAdmin)
like image 159
Sumit S Chawla Avatar answered Oct 31 '25 06:10

Sumit S Chawla



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!