Reputation: 107
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)
Upvotes: 4
Views: 14187
Reputation: 3580
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)
Upvotes: 10
Reputation: 131
Why don't you try this in admin.py:
from django.contrib import admin
class HomeAdmin(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
Upvotes: 5