我一直在尝试创建此模型对象,但不断收到此错误:字段“id”需要一个数字,但得到“创建”。错误图片分享如下。我正在使用 djangi 3.0.3 view.py 文件:-
from django.shortcuts import render
from . import models
from django.views.generic import (View,TemplateView,ListView,DetailView,
CreateView,UpdateView,DetailView)
# Create your views here.
class IndexView(TemplateView):
template_name = 'index.html'
class SchoolListView(ListView):
context_object_name = 'schools'
model = models.School
class SchoolDetailView(DetailView):
context_object_name = 'school_detail'
model = models.School
template_name = 'basic_app/school_detail.html'
class SchoolCreateView(CreateView):
fields = ('name','principal','location')
model = models.School
model.py
from django.db import models
from django.urls import reverse
# Create your models here.
class School(models.Model):
name = models.CharField(max_length=265)
principal = models.CharField(max_length=256)
location = models.CharField(max_length=256)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse("basic_app:detail", kwargs={'pk':self.pk})
class Student(models.Model):
name = models.CharField(max_length=256)
age = models.PositiveIntegerField()
school = models.ForeignKey(School, related_name='student', on_delete=models.CASCADE)
def __str__(self):
return self.name
慕尼黑5688855
相关分类