【问题标题】:after I created multiple models in my django app for the restframwork I start getting more errs在我的 django 应用程序中为 restframwork 创建多个模型后,我开始收到更多错误
【发布时间】:2020-08-07 07:07:06
【问题描述】:

我创建了一个模型 django 项目然后我添加了两个模型然后我得到了这个错误

RecursionError at /elements/
maximum recursion depth exceeded while calling a Python object

然后我重新安装了一些软件包,例如 djangorestframwork-recursion,然后我得到了这个错误

Error at /
Incorrect padding
Request Method: GET
Request URL:    http://127.0.0.1:8000/
Django Version: 3.0.8
Exception Type: Error
Exception Value:    
Incorrect padding
Exception Location: //anaconda3/lib/python3.7/base64.py in b64decode, line 87
Python Executable:  //anaconda3/bin/python
Python Version: 3.7.3
Python Path:    
['/Users/apple/Desktop/Trying to clone notionso note taking app with django '
 'and reac1/backend/restapi',
 '//anaconda3/lib/python37.zip',
 '//anaconda3/lib/python3.7',
 '//anaconda3/lib/python3.7/lib-dynload',
 '/Users/apple/.local/lib/python3.7/site-packages',
 '//anaconda3/lib/python3.7/site-packages',
 '//anaconda3/lib/python3.7/site-packages/aeosa']
Server time:    Fri, 7 Aug 2020 06:59:57 +0000

当我运行 python manage.py makemigrations 我得到了

You are trying to change the nullable field 'text' on components to non-nullable without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
 1) Provide a one-off default now (will be set on all existing rows with a null value for this column)
 2) Ignore for now, and let me handle existing rows with NULL myself (e.g. because you added a RunPython or RunSQL operation to handle NULL values in a previous data migration)
 3) Quit, and let me add a default in models.py
Select an option: 

nullable field 'text' 有什么问题(我也将每个null=Flase 更改为null=True 我得到了同样的错误。

models.py

# this is a recursive model
from django.db import models


class elements(models.Model):
    tag = models.CharField(blank=True, null=True, max_length=20)
    text = models.TextField(blank=True, null=False)
    src = models.URLField(blank=True, null=True)
    # problem: styles replace spaces with slashes (/)
    # I tried: json field but did'nt work.
    style = models.TextField(blank=True, null=True)

    main = models.ForeignKey(
        'self', null=True, blank=True, related_name="sub", on_delete=models.PROTECT)


class styles(models.Model):
    name = models.TextField(blank=True, null=True, max_length=50)
    style = models.TextField(blank=True, null=True)
    main = models.ForeignKey(
        'self', null=True, blank=True, related_name="sub", on_delete=models.PROTECT)


class components(models.Model):
    name = models.TextField(blank=True, null=True, max_length=50)
    tag = models.CharField(blank=True, null=True, max_length=20)
    text = models.TextField(blank=True, null=False)
    src = models.URLField(blank=True, null=True)
    style = models.TextField(blank=True, null=True)

    main = models.ForeignKey(
        'self', null=True, blank=True, related_name="sub", on_delete=models.PROTECT)

serialzers.py

from rest_framework import serializers
from models.models import elements, styles, components
from rest_framework_recursive.fields import RecursiveField


class elementsSerializer(serializers.ModelSerializer):
    sub = RecursiveField(many=True, required=False)

    class Meta:
        model = elements
        fields = ('id', 'tag', 'text', 'src', 'style', 'main', 'sub')


class stylesSerializer(serializers.ModelSerializer):
    sub = RecursiveField(many=True, required=False)

    class Meta:
        model = styles
        fields = ('id', 'name', 'style', 'main', 'sub')


class componentsSerializer(serializers.ModelSerializer):
    sub = RecursiveField(many=True, required=False)

    class Meta:
        model = components
        fields = ('id', 'name', 'tag', 'text', 'src', 'style', 'main', 'sub')

view.py

from models.models import elements, styles, components
from .serializers import elementsSerializer, stylesSerializer, componentsSerializer
from rest_framework import viewsets


class elementsVeiwSet(viewsets.ModelViewSet):
    serializer_class = elementsSerializer
    queryset = elements.objects.all()


class stylesVeiwSet(viewsets.ModelViewSet):
    serializer_class = stylesSerializer
    queryset = styles.objects.all()


class componentsVeiwSet(viewsets.ModelViewSet):
    serializer_class = componentsSerializer
    queryset = components.objects.all()

urls.py

from models.models import elements, styles, components
from .serializers import elementsSerializer, stylesSerializer, componentsSerializer
from rest_framework import viewsets


class elementsVeiwSet(viewsets.ModelViewSet):
    serializer_class = elementsSerializer
    queryset = elements.objects.all()


class stylesVeiwSet(viewsets.ModelViewSet):
    serializer_class = stylesSerializer
    queryset = styles.objects.all()


class componentsVeiwSet(viewsets.ModelViewSet):
    serializer_class = componentsSerializer
    queryset = components.objects.all()

【问题讨论】:

    标签: django django-rest-framework rest


    【解决方案1】:

    1) 我不确定你为什么在每个模型中都有这个字段

    main = models.ForeignKey('self', null=True, blank=True, related_name="sub", on_delete=models.PROTECT)
    

    我认为这是您的递归问题的原因。尝试删除它。

    2) 关于可空字段text 的问题,这是因为您没有提供默认值(您必须这样做,因为您不允许它为空)。你这样做:

    text = models.TextField(blank=True, null=False, default='')
    

    【讨论】:

    • 对我来说最好的办法是重新创建项目-_-
    • @AliHusham 这根本不需要,也不会改变任何东西。当您真正陷入困境时,您唯一应该删除的是您的迁移文件。在最坏的情况下,您会删除数据库并从头开始迁移。
    • 我发现我在 .gitignore 中设置了 env 文件夹,所以这是环境问题,我是初学者。另外,如何删除我的迁移?还是sql的数据?
    • 是的,你的环境应该在 gitignore 中。但这不会在您的本地项目文件中删除它。您可以通过删除应用目录migrations 下的所有文件(__init__.py 除外)来删除迁移。要删除您的数据库,只需删除文件(例如 db.sqlite3 文件。
    猜你喜欢
    • 2016-06-16
    • 1970-01-01
    • 2021-08-26
    • 2021-06-08
    • 2021-06-17
    • 1970-01-01
    • 2015-12-13
    • 2010-09-30
    • 2020-11-23
    相关资源
    最近更新 更多