尝试在Django中迁移时出现奇怪的错误

时间:2020-02-01 01:03:54

标签: python django

我正在尝试在Django中创建一个todo-app。一切顺利,直到出现以下错误:

:(admin.E108)'list_display [2]'的值表示'due_date',该值不可调用,'TodoListAdmin'的属性或'todolist.TodoList'的属性或方法。 / p>

模型文件:

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和此网站都是陌生的。预先感谢。

1 个答案:

答案 0 :(得分:2)

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