python - Django 管理站点添加“城市对象”而不是“城市”
问题描述
我正在尝试在 Django 上创建一个基本的天气预报网站,但出现了这个错误。我成功创建了“城市”部分,但是在添加任何城市名称时,会添加“城市对象 1”。这会在从 API 密钥读取 JSON 时产生错误。
from django.db import models
class City(models.Model):
name = models.CharField(max_length=25)
def _str_(self): #show the actual city name on the dashboard
return self.name
class Meta: #show the plural of city as cities instead of citys
verbose_name_plural = 'cities'
管理员.py
from django.contrib import admin
from .models import City
admin.site.register(City)
解决方案
两个下划线
def __str__(self):
https://docs.djangoproject.com/en/3.0/ref/models/instances/#other-model-instance-methods
推荐阅读
- java - HibernateException:无法访问 lob 流
- asp.net-core - 如何在 asp.net 核心启动中保存信息以在应用程序中可用
- ajax - POST 请求在 chrome 中没有得到服务器的响应,但在邮递员中工作
- r - 基于第二个空格的条件数据帧字符串拆分
- android - 初始屏幕上显示的导航栏和信封图标
- javascript - 请求ajax findOne
- c++ - 如何比较两个时间时间戳
- ios - 无法构建 Objective-C 模块“CoreFoundation”
- c++ - 如何将 cin 从 main 中继到 main 之上的函数和结构?
- google-app-maker - 如何播种生产数据库