How To Display All My Model's Fields In Django Admin?
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 dj
Solution 1:
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:
classHome(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
classHomeAdmin(admin.ModelAdmin):
list_display = ('image', 'paragraph','created',)
admin.site.register(Home,HomeAdmin)
Solution 2:
Why don't you try this in admin.py:
from django.contrib import admin
classHomeAdmin(admin.ModelAdmin):
list_display = ('paragraph', 'created', 'image', )
admin.site.register(Home, HomeAdmin)
What this should do is show the fields and their values when viewing a list of Home objects in the admin. So this should only work for list view in admin.
Also, if you have trouble viewing the image from the image field in admin, then checkup on the answers to the following question: Django Admin Show Image from Imagefield
Post a Comment for "How To Display All My Model's Fields In Django Admin?"