ModelForm 和單元測試的奇怪問題:TypeError: getattr(): 屬性名稱必須是字符串

神奇的魚

知道這裡會發生什麼:

模型.py

class Sound(models.Model):
    SEX_CHOICES = (
            ('M', 'Male'),
            ('F', 'Female')
    )
    phrase1 = models.CharField(max_length=300)
    phrase2 = models.CharField(max_length=300)
    language = models.CharField(max_length=50)
    num_words = models.PositiveSmallIntegerField(verbose_name="number of words")
    num_syllables = models.PositiveSmallIntegerField(verbose_name="number of syllables")
    sex = models.CharField(max_length=1, choices=SEX_CHOICES)
    voice = models.BooleanField(verbose_name="synthetic voice")
    speaker_name = models.CharField(max_length=50)
    sound_hash = models.CharField(max_length=40, primary_key=True, editable=False)
    key = models.CharField(max_length=200, editable=False, default="", null=True, help_text="Amazon S3 key")

表格.py

from django import forms
class LessonSoundForm(forms.ModelForm):
    class Meta:
        model = Sound
        fields = ["phrase1", "phrase2", "num_words", "num_syllables"]

測試.py

from django.test import TestCase
from forms import LessonSoundForm
class LessonSoundForm(TestCase):
    def setUp(self):
        self.sound_data = {
                "phrase1": "Poop",
                "phrase2": "Knuckle",
                "num_words": 1,
                "num_syllables": 1
        }

    def test_lesson_sound_form_validates_with_good_data(self):
        form = LessonSoundForm(data=self.sound_data)
        self.assertTrue(form.is_valid())

這看起來非常簡單,所有其他測試都使用ModelForm. 但是,我收到此錯誤:

line 20, in test_lesson_sound_form_validates_with_good_data
        form = LessonSoundForm(data=self.sound_data)
    TypeError: __init__() got an unexpected keyword argument 'data'

如果我在沒有data參數的情況下運行它,我會得到:

    form = LessonSoundForm(self.sound_data)
  File "/usr/lib/python3.6/unittest/case.py", line 397, in __init__
    testMethod = getattr(self, methodName)
TypeError: getattr(): attribute name must be string

這似乎是一個非常基本的案例,我知道會出現什麼問題。我認為這只是一天的結束,我錯過了一些愚蠢的事情。

xyres
from forms import LessonSoundForm
                  ^^^^^^^^^^^^^^^
class LessonSoundForm(TestCase):
      ^^^^^^^^^^^^^^^

LessonSoundForm由於相同的 name ,當您為測試用例定義新類時,您正在覆蓋

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

getattr和unicode属性

来自分类Dev

为什么在getattr()中将属性作为字符串传递?

来自分类Dev

使用 getattr 获取字符串变量的内容

来自分类Dev

python中非常奇怪的__getattr__行为

来自分类Dev

python中非常奇怪的__getattr__行为

来自分类Dev

元类的__getattr__没有被调用

来自分类Dev

元类的__getattr__没有被调用

来自分类Dev

GetAttr 在元类中引发 AttributeErorr

来自分类Dev

装饰方法上的getattr生成TypeError

来自分类Dev

将ModelForm数据转换为JSON字符串

来自分类Dev

在Django modelform中将字符串转换为日期时间

来自分类Dev

Django:如何使用ModelForm和通用视图

来自分类Dev

Django modelForm和html模板-安全关注

来自分类Dev

Django modelForm和html模板-安全关注

来自分类Dev

Python 3 getattr字符串命名为什么不好?

来自分类Dev

TypeError-使用getattr缺少必需的位置参数

来自分类Dev

使用 __getattr__ 导致 TypeError: 'str' object is not callable - Python 2.7

来自分类Dev

Python继承和__getattr__和__getattribute__

来自分类Dev

Python继承和__getattr__和__getattribute__

来自分类Dev

AttributeError的属性和__getattr__兼容性问题

来自分类Dev

为什么要使用内置的setattr()和getattr()?

来自分类Dev

如何同时实现__getitem__和__getattr__?

来自分类Dev

python中的getattr(self,'__a')和self .__ a有什么区别?

来自分类Dev

AttributeErrors:@property和__getattr__之间的不良交互

来自分类Dev

在Python描述符中使用setattr()和getattr()

来自分类Dev

如何避免在ModelForm和CreateView类中重复字段列表?

来自分类Dev

具有ModelChoiceField和Mock数据的UnitTest ModelForm

来自分类Dev

django-使用FormView和ModelForm更新模型

来自分类Dev

如何在ModelForm中指定Select和RadioSelect?

Related 相关文章

  1. 1

    getattr和unicode属性

  2. 2

    为什么在getattr()中将属性作为字符串传递?

  3. 3

    使用 getattr 获取字符串变量的内容

  4. 4

    python中非常奇怪的__getattr__行为

  5. 5

    python中非常奇怪的__getattr__行为

  6. 6

    元类的__getattr__没有被调用

  7. 7

    元类的__getattr__没有被调用

  8. 8

    GetAttr 在元类中引发 AttributeErorr

  9. 9

    装饰方法上的getattr生成TypeError

  10. 10

    将ModelForm数据转换为JSON字符串

  11. 11

    在Django modelform中将字符串转换为日期时间

  12. 12

    Django:如何使用ModelForm和通用视图

  13. 13

    Django modelForm和html模板-安全关注

  14. 14

    Django modelForm和html模板-安全关注

  15. 15

    Python 3 getattr字符串命名为什么不好?

  16. 16

    TypeError-使用getattr缺少必需的位置参数

  17. 17

    使用 __getattr__ 导致 TypeError: 'str' object is not callable - Python 2.7

  18. 18

    Python继承和__getattr__和__getattribute__

  19. 19

    Python继承和__getattr__和__getattribute__

  20. 20

    AttributeError的属性和__getattr__兼容性问题

  21. 21

    为什么要使用内置的setattr()和getattr()?

  22. 22

    如何同时实现__getitem__和__getattr__?

  23. 23

    python中的getattr(self,'__a')和self .__ a有什么区别?

  24. 24

    AttributeErrors:@property和__getattr__之间的不良交互

  25. 25

    在Python描述符中使用setattr()和getattr()

  26. 26

    如何避免在ModelForm和CreateView类中重复字段列表?

  27. 27

    具有ModelChoiceField和Mock数据的UnitTest ModelForm

  28. 28

    django-使用FormView和ModelForm更新模型

  29. 29

    如何在ModelForm中指定Select和RadioSelect?

热门标签

归档