- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在我的 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/
背景 我在 Django Cache Machine 中发现了一个相当严重的错误,它在从 Django 1.4 升级到 1.7 后导致它的失效逻辑失去理智。 该错误被本地化为在扩展缓存机器的 Cach
我正在尝试通过管理员保存模型,但我一直收到此错误 unsupported operand type(s) for +: 'DeferredAttribute' and 'str'". 我想返回模型的字
在我的 models.py 中,我有以下类(class): class AvailabilityTypes(): STUDYING = 'STUDYING' WORKING = 'WO
经过几天的寻找,我仍然无法跨过这个障碍。我只是想打印一份来自卖家的描述列表作为 View 。这是我正在使用的... 模型.py: from django.db import models class
我收到错误,AttributeError: 'DeferredAttribute' 对象没有属性 'objects'。我想解析 Excel 并将其放入 model(City&Prefecture&Ar
我是 Wagtail 和 Django 的新手,我正在尝试构建一个模型,该模型将从 REST API 中提取数据并将其放入可以在模板上迭代的对象中。但是,在尝试迁移时,出现此错误: relat
我是 django 的新手。我真的对 View 过滤器感到困惑。 这是我的models.py: class Author(models.Model): title = models.CharF
这是我第一次使用 Django REST FRAMEWORK,我遇到了注册 api 的问题,第一次尝试时 api 工作正常,但在接下来的尝试中,它开始抛出此错误 AttributeError: 'De
免责声明:我对 Django 相当陌生(试图自学),目前正在尝试获取一个几乎没有文档的包 https://github.com/byteweaver/django-coupons为我正在构建的应用程序
我想将所有模型字段写入一个文本文件,但我得到:我该如何解决这个问题?我正在制作患者注册表,注册后我想查看文本文件中的所有模型字段。代码有效,我得到一个文本文件,但我看到的不是预期值,而是一个延迟属性。
我在 Django 中使用查询集 - 每当我运行服务器时,它都会给出错误。 TypeError: Field 'id' expected a number but got get_user_model
我在 django Rest freamework 应用程序中创建了新模型,但收到此错误消息: 类型错误:% 不支持的操作数类型:“DeferredAttribute”和“dict” 这是错误截图:
免责声明:我只是一个尝试学习 Django 的新手 您好,我正在尝试重构我的代码并将我创建的所有 View 修改为基于类的 View 。 我在加载带有 DeleteView 的表单时遇到问题,该表单显
我是一名优秀的程序员,十分优秀!