首页 > 解决方案 > 尝试在 Django 中迁移时出现奇怪的错误

问题描述

我正在尝试在 Django 中制作一个待办事项应用程序。一切顺利,直到我收到以下错误:

: (admin.E108) 'list_display[2]' 的值是指'due_date',它不是可调用的,不是'TodoListAdmin' 的属性,也不是'todolist.TodoList' 上的属性或方法。

模型文件:

from django.db import models
from django.utils import timezone
# Create your models here.

class Category(models.Model): #The Category table name that inherits models.Model
    name = models.CharField(max_length=100)#Like a varchar

    class Meta:
        verbose_name = ("Category")
        verbose_name_plural = ("Categories")

    def __str__(self):
        return self.name #name to be shown when called(Whatever tf that means)

class TodoList(models.Model): #Todolist able that inherits models.Model
    title = models.CharField(max_length=250) #This is apparently a varchar
    content = models.TextField(blank=True) #A text field
    created = models.DateField(default=timezone.now().strftime("%Y-%m-%d")) #Presents a date.
    category = models.ForeignKey(Category, on_delete=models.PROTECT, default="general") #A foreignkey.

    class Meta:
        ordering = ["-created"] #ordering by the created field.

    def __str__(self):
        return self.title #Name to be sown when called.

管理文件:

from django.contrib import admin
from . import models
# Register your models here.
class TodoListAdmin(admin.ModelAdmin):
    list_display = ("title", "created", "due_Date")

class CategoryAdmin(admin.ModelAdmin):
    list_display = ("name",)

admin.site.register(models.TodoList, TodoListAdmin)
admin.site.register(models.Category, CategoryAdmin)

视图文件:

from django.shortcuts import render,redirect
from .models import TodoList, Category
# Create your views here.
def index(request): #the index-view.
    todos = TodoList.objects.all() # querying all todos with the object manager.
    categories = Category.objects.all()#Gets all categories, using the object-manager.
    if request.method == "POST": #Checks if the request-method is a POST
        if "taskAdd" in request.POST: #Checks if there is a request to add a todo
            title = request.POST["description"] #Title
            date = str(request.POST["date"]) #date
            category = request.POST["category_select"] #category
            content = title + "--" + date + "" + category #Adds the previously defined variables together to form the content-variable.
            Todo = TodoList(title=title, content=content, due_date=date, category=Category.objects.get(nae=category))
            Todo.save() #saving the todo
            return redirect("/") #Reloads the page

        if "taskDelete" in request.POST: #Checks if there is a request to delete a todo.
            checklist = request.POST["checkedbox"] #checked todos to be deleted.
            for todo_id in checkedlist:
                todo = TodoList.objects.get(id=int(todo_id)) #gets id of todo.
                todo.delete() #deletes the todo in question.
        return render(request, "index.html", {"todos" : todos, "categories":categories})

我实际上不知道这些是否相关,所以如果您可能需要了解其他任何内容,请告诉我。你可能会说,我对 Django 和这个网站都很陌生。提前致谢。

标签: pythondjango

解决方案


在您的 TodoList 模型中,您没有 due_Date 字段,这就是您收到此错误的原因:“list_display[2]”的值是指“due_date”,它不是可调用的。admin 中的 list_display 仅采用模型的字段名称。我不知道您为什么使用模型中不存在的due_date。


推荐阅读