Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: show useful database data in admin interface?

So I've set up my django site with the following admin.py:

import models
from django.contrib import admin
admin.site.register(models.Comment)

which uses this models.py:

from django.db import models
class Comment(models.Model):
    text = models.CharField(max_length=400)
    name = models.CharField(max_length=100)
    date = models.DateTimeField(auto_now = True)
    article = models.CharField(max_length=100)

However, when I go to the admin page, it shows the following:

enter image description here Which generally is not very helpful. Clicking on each link gives me a page with that object's data, but I would like to be able to see the information for each object in this view. I've been looking at the ModelAdmin class at:

https://docs.djangoproject.com/en/dev/ref/contrib/admin/

but have not managed to wrap my head around it. Is it a separate model class that needs to be kept in sync with my "actual" model? Is it just an interface through which my Admin site accesses the actual model? Does it do what I want (allowing useful data to be shown in the admin interface) or does it do something else?

I'm thinking that the Django Admin page should be able to replace PHPMyAdmin for doing simple tasks, like browsing the DB and manually modifying individual objects. Is that the case?

like image 848
Li Haoyi Avatar asked Apr 27 '26 11:04

Li Haoyi


1 Answers

The admin turns your object into a string so just put a def __str__ or def __unicode__

(As @Mandax has reminded me the docs recommend to define __unicode__ only.)

def __unicode__(self);
    return u"%s (%s): %s" % (self.article, self.date, self.name)
like image 132
James Khoury Avatar answered Apr 29 '26 01:04

James Khoury



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!