我尝试从管理面板添加数据,但失败了Django

时间:2020-03-15 22:15:48

标签: python django django-rest-framework

我尝试将数据添加为admin。我进入url / admin / festival / festival / add 并显示表单。我完成了表单,但是当我单击保存时,它显示了IntegrityError,表示外键约束失败。但是我在模型中没有外键。我该如何解决?

IntegrityError at /admin/festival/festival/add/

FOREIGN KEY constraint failed

Request Method:     POST
Request URL:    http://127.0.0.1:8000/admin/festival/festival/add/
Django Version:     3.0.4
Exception Type:     IntegrityError
Exception Value:    

FOREIGN KEY constraint failed

Exception Location:     /home/anamaria/workspace/AllFest2/venv/lib/python3.6/site-packages/django/db/backends/base/base.py in _commit, line 243
Python Executable:  /home/anamaria/workspace/AllFest2/venv/bin/python
Python Version:     3.6.9
Python Path:    

['/home/anamaria/workspace/AllFest2/festivals',
 '/usr/lib/python36.zip',
 '/usr/lib/python3.6',
 '/usr/lib/python3.6/lib-dynload',
 '/home/anamaria/workspace/AllFest2/venv/lib/python3.6/site-packages']

这是 models.py

from django.db import models
from jsonfield import JSONField


class Festival(models.Model):
    name = models.CharField(max_length=100)
    start_date = models.DateTimeField(blank=True, null=True, default=None)
    end_date = models.DateTimeField(blank=True, null=True, default=None)
    number_of_tickets = models.IntegerField()
    location = JSONField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.name

views.py 中,我有

from .serializers import FestivalSerializer
from .permissions import FestivalPermission
from rest_framework import mixins, status
from festival.models import Festival
from rest_framework.viewsets import GenericViewSet


class FestivalViewSet(mixins.CreateModelMixin, mixins.ListModelMixin, mixins.RetrieveModelMixin, mixins.UpdateModelMixin,GenericViewSet):
    serializer_class = FestivalSerializer
    permission_classes = [FestivalPermission]
    queryset = Festival.objects.all()
serializers.py 中的

serializer:

from rest_framework import serializers
from festival.models import Festival


class FestivalSerializer(serializers.BaseSerializer):

    class Meta:
        model = Festival
        fields = '__all__'

admin.py 文件:

from django.contrib import admin
from .models import Festival

class FestivalAdmin(admin.ModelAdmin):
    list_display = ('name', )

    def save_model(self, request, obj, form, change):
        super(FestivalAdmin, self).save_model(request, obj, form, change)

# Register your models here.
admin.site.register(Festival, FestivalAdmin)

0 个答案:

没有答案