简体   繁体   English

Django:在管理界面中显示有用的数据库数据?

[英]Django: show useful database data in admin interface?

So I've set up my django site with the following admin.py: 所以我用以下admin.py设置了我的django网站:

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

which uses this models.py: 它使用这个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: 但是,当我转到管理页面时,它显示以下内容:

在此输入图像描述 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: 我一直在寻找ModelAdmin类:

https://docs.djangoproject.com/en/dev/ref/contrib/admin/ 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. 我认为Django Admin页面应该能够替换PHPMyAdmin来执行简单的任务,比如浏览数据库并手动修改单个对象。 Is that the case? 是这样的吗?

The admin turns your object into a string so just put a def __str__ or def __unicode__ 管理员将你的对象变成一个字符串,所以只需输入一个def __str__def __unicode__

(As @Mandax has reminded me the docs recommend to define __unicode__ only.) (正如@Mandax提醒我, 文档建议仅定义__unicode__ 。)

def __unicode__(self);
    return u"%s (%s): %s" % (self.article, self.date, self.name)

Just as it says in the documentation , your model's ModelAdmin describes how the admin section will represent your model. 正如文档中所述,您的模型的ModelAdmin描述了管理部分将如何表示您的模型。 It does need to be somewhat in sync with the actual model, it doesn't make sense for you to display fields that aren't present on your model, etc. You seem interested in the changelist view, which has numerous customization options (all described in the documentation, and in the tutorial ). 它确实需要与实际模型有一定的同步,显示模型中不存在的字段等没有意义。您似乎对changelist视图感兴趣,该视图有许多自定义选项(全部在文档和教程中描述。 A simple start might be: 一个简单的开始可能是:

from django.contrib import admin

class CommentAdmin(admin.ModelAdmin):
    # define which columns displayed in changelist
    list_display = ('text', 'name', 'date', 'article')
    # add filtering by date
    list_filter = ('date',)
    # add search field 
    search_fields = ['text', 'article']

admin.site.register(Comment, CommentAdmin)

There are a lot of options for customization, as always refer to the docs! 有很多自定义选项,因为总是参考文档! Finally, you could certainly use it in lieu of PHPMyAdmin, it's very easy to setup admin for browsing, modifying object, etc, how much use you get out of it is up to you. 最后,您当然可以使用它来代替PHPMyAdmin,它可以很容易地设置管理员进行浏览,修改对象等,您可以从中获得多少用途取决于您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM