python - 类型错误 : 'DeferredAttribute' object is not iterable

标签 python django

在我的 models.py 中,我有以下类(class):

class AvailabilityTypes():
    STUDYING = 'STUDYING'
    WORKING = 'WORKING'
    SEARCHING = 'SEARCHING'
    FREELANCER = 'FREELANCER'

    types = (
        (STUDYING, 'Estudando'),
        (WORKING, 'Trabalhando'),
        (SEARCHING, 'Procurando por emprego'),
        (FREELANCER, 'Freelancer')
    )

    def get_types(self):
        return self.types.all()

我想在 Django 表单中显示该选项。在我的 forms.py 文件中,我有以下代码:

from django import forms
from .models import AvailabilityTypes

[...]

availability = forms.CharField(
        widget=forms.ChoiceField(
            choices=(AvailabilityTypes.types)
        )
    )

但我收到错误 TypeError: 'DeferredAttribute' object is not iterable。我究竟做错了什么?另外,如果我尝试使用:

availability = forms.CharField(
        widget=forms.ChoiceField(
            choices=(AvailabilityTypes.get_types())
        )
    )

我收到错误 TypeError: get_types() 缺少 1 个必需的位置参数:'self'。

我是 Django 和 Python 的新手,我需要一些帮助。谢谢。

最佳答案

Sol 1.修复当前代码

首先修复您的方法get_types():

class AvailabilityTypes():
...
# Above this Same as your code

def get_types(self):
    return self.types  # You need to return just types, it is a tuple, it doesn't has an attribute all(). In Django we usually use all() in querysets.

现在修复表单:

from django import forms
from .models import AvailabilityTypes

at_obj = AvailabilityTypes()  # Create an object of class AvailabilityTypes

[...]  # Guessing form class starts here

availability = forms.CharField(
    widget=forms.ChoiceField(
        # choices=AvailabilityTypes.get_types()  # You can't call a class's method on that class, you call it on that class's object
        choices=(at_obj.get_types())  # Call the method on object of the class not class itself
    )
)

Sol 2.不要创建不必要的类

在 models.py 中,无需创建一个类来保存类型。你可以这样做:

...
# All your imports go above this
# These four variables are pointless in my opinion, but I will leave them be
STUDYING = 'STUDYING'
WORKING = 'WORKING'
SEARCHING = 'SEARCHING'
FREELANCER = 'FREELANCER'

# types is an constant so it should follow uppercase naming style
TYPES = (  
    (STUDYING, 'Estudando'),
    (WORKING, 'Trabalhando'),
    (SEARCHING, 'Procurando por emprego'),
    (FREELANCER, 'Freelancer')
)

# Create your models Here
...

现在在你的 forms.py 中:

...
from .models import TYPES  # Import the tuple from models

[...]

availability = forms.CharField(
    widget=forms.ChoiceField(choices=TYPES)  # Use the imported tuple directly here
)

Sol 3.使用模型形式(最简单、最容易)

在您的模型中:

from django.db import models

# types is an constant so it should follow uppercase naming style
TYPES = (  
    ('STUDYING', 'Estudando', ),
    ('WORKING', 'Trabalhando', ),
    ('SEARCHING', 'Procurando por emprego', ),
    ('FREELANCER', 'Freelancer', ),  # Unlike other languages your last element can have a trailing comma too, its optional but still do that. I has some advantages which I am not gonna explain here
)

# Create your models Here
class MyModel(models.Model):
    availability = models.CharField(max_length=63, choices=TYPES)
    # Other fields of your model
    ...

现在在你的 forms.py 中:

from django import forms
from .models import MyModel

class MyModelForm(forms.ModelForm):
    class Meta:
        model = MyModel
        fields = ('availability', )  # add other fields of your models too in the tuple

就是这样。您已完成,在 View 中使用您的表单。 Django 将负责显示正确的选择、验证它们、显示相关错误消息并将有效数据保存在数据库中。

关于python - 类型错误 : 'DeferredAttribute' object is not iterable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52854144/

相关文章:

python - Selenium 无法切换选项卡和提取 url

python - AWS Sagemaker 推理端点未利用所有 vCPU

python - Django Rest Framework - 如何编写多个嵌套字段序列化器(用于读取和写入)

android - 如何从 Android 应用程序将带有 POST 请求的 token 发送到 Django REST api?

django manytomany模型关系在对象创建时崩溃管理

python - 导入文件全部内容的 Python 语法是什么?

python - 该页码小于 1 Django

python - 无法让我的脚本向右滑动按钮

python - 多维Newton Raphson的同时优化/时间复杂度

python - 在Python中高效创建字典并读取查询字典的输入