python - 请更正以下错误我无法在模型中添加数据
问题描述
有人可以建议我做错了什么吗?我总是得到错误:
模型.py
from django.db import models
from datetime import datetime
from ckeditor.fields import RichTextField
# Create your models here.
class Youtuber(models.Model):
crew_choices = (
('solo','solo'),
('small','small'),
('large','large'),
)
camera_choices = (
('canon','canon'),
('nikon','nikon'),
('sony','sony'),
('red','red'),
('fuji','fuji'),
)
category_choices = (
('code','code'),
('mobile_review','mobile_review'),
('vlogs','vlogs'),
('comedy','comedy'),
('gaming','gaming'),
)
name=models.CharField(max_length=254)
price = models.IntegerField()
photo = models.ImageField(upload_to='media/ytubers/%Y/%m')
video_url = models.CharField(max_length=255)
description = RichTextField()
city =models.CharField(max_length=255)
age =models.IntegerField()
height = models.IntegerField()
crew =models.CharField(choices=crew_choices, max_length=255)
camera_type =models.CharField(choices=camera_choices, max_length=255)
subs_count =models.CharField(max_length=255)
category =models.BooleanField(choices=category_choices, default=False)
is_featured =models.BooleanField(default=False)
created_date =models.DateTimeField(default = datetime.now, blank=True )
管理员.py
from django.contrib import admin
from .models import Youtuber
from django.utils.html import format_html
# Register your models here.
class YtAdmin(admin.ModelAdmin):
# def myphoto(self,object):
# return format_html('<img src="{}" width="40" />.format(object.photo.url)')
list_display = ('id','name','subs_count','is_featured')
search_fields = ('name','camera_type')
list_filter = ('city','camera_type')
list_dispaly_link = ('id','name')
list_editable = ('is_featured',)
admin.site.register(Youtuber,YtAdmin)
解决方案
您使用 models.BooleanField 作为类别,因此唯一可能的值是 True 和 False。将其更改为 models.Charfield 并且它应该可以工作。
这个:
category =models.BooleanField(choices=category_choices, default=False)
应该:
category = models.CharField(choices=category_choices, max_length=255)
推荐阅读
- javascript - 有没有办法跨多个页面共享一个表(不使用本地存储)?
- flutter - 播放视频网络所需的安全标头
- firebase - 如果收藏夹部分中不存在新产品的 ID,我想向 firebase 添加新产品
- node.js - Mongoose 模式方法返回不是函数
- apache-beam - 请问beam中的“p”是什么?
- spring-boot - vue axios第一个请求带有jsessionId
- python - python中没有for循环的矩阵计算
- javascript - 通过单击表格行重定向到烧瓶路由功能
- apache-spark - 控制 Spark 中的小数精度溢出
- ios - 重新启动应用程序时,中央取消订阅特征