0

Ok, so I am trying verify in my unittest that a certain field is a ManyToManyField object. Here is the original class, followed by the relevant unit testing code:

class Course(models.Model):
    name = models.CharField(max_length=150)
    studyunits = models.ManyToManyField(StudyUnit)

class CourseManyToManyTest(VocabTestCase):
    fixtures = ['m2mtest.json']

    def setUp(self):
        self.course = Course.objects.create(name="Course2")
        self.studyunit1 = StudyUnit.objects.all()[0]
        self.studyunit2 = StudyUnit.objects.all()[1]

    def testStudyUnitsAddDelete(self):
        self.assertIsInstance(self.course.studyunits, models.ManyToManyField)

That test fails with the following error:

self.assertIsInstance(self.course.studyunits, models.ManyToManyField)
AssertionError: <django.db.models.fields.related.ManyRelatedManager object at 0x102853690>      is not an instance of <class 'django.db.models.fields.related.ManyToManyField'>

I know it seems stupid to verify this, but I found this bug in trying to extend the ManyToManyField, and now I am curious why the object's class changes. Seems a bit too magical, what is the explanation?

4

1 回答 1

0

Django 中的字段是描述符,它们不返回自身,而是该字段保存或表示的对象(例如,IntegerField返回整数)。

于 2012-08-05T04:33:53.297 回答