gpt4 book ai didi

django - 在管理员中我看到 "App_name object"但不是实际的对象名称

转载 作者:行者123 更新时间:2023-12-04 18:09:06 28 4
gpt4 key购买 nike

所以,我通过这个学习 django http://mherman.org/blog/2012/12/30/django-basics/教程,我有一个问题。

我在数据库中添加了几本书,但在管理站点中我只看到“App_name 对象”。在我的例子中,我只看到单词列表“Books object”、“Books object”、“Books object”,而实际上我应该看到“War and Peace”、“Brave New World”、“To Kill a Mockingbird”。

那么,你知道我的应用有什么问题吗?

谢谢;)

已编辑:添加我的 models.py 代码

from django.db import models

class Books(models.Model):
title = models.CharField(max_length=150)
author = models.CharField(max_length=100)
read = models.CharField(max_length=3)

def __unicode__(self):
return self.title + " / " + self.author + " / " + self.read

我找到了答案:

Django 1.5 has experimental support for Python 3, but the Django 1.5 tutorial is written for Python 2.X:

This tutorial is written for Django 1.5 and Python 2.x. If the Django version doesn’t match, you can refer to the tutorial for your version of Django or update Django to the newest version. If you are using Python 3.x, be aware that your code may need to differ from what is in the tutorial and you should continue using the tutorial only if you know what you are doing with Python 3.x.

In Python 3, you should define a str method instead of a unicode method. There is a decorator python_2_unicode_compatible which helps you to write code which works in Python 2 and 3.

@python_2_unicode_compatible class Poll(models.Model): question = models.CharField(max_length=200) pub_date = models.DateTimeField('date published')

def __str__(self):
return self.question For more information see the section str and unicode methods in the Porting to Python 3 docs.

最佳答案

您尚未定义(或未正确定义)Books 模型的 __unicode__() 方法:

5. Next, open up your models.py file and add these two lines of code-

 def __unicode__(self):
return self.title + " / " + self.author + " / " + self.read

仅供引用,引自 docs :

The __unicode__() method is called whenever you call unicode() on an object. Django uses unicode(obj) (or the related function, str(obj)) in a number of places. Most notably, to display an object in the Django admin site and as the value inserted into a template when it displays an object. Thus, you should always return a nice, human-readable representation of the model from the __unicode__() method.

关于django - 在管理员中我看到 "App_name object"但不是实际的对象名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18727460/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com