如何在Django测试中传递ManyToMany值?

时间:2015-08-17 17:32:50

标签: python django django-models django-tests

如何在测试中为ManyToMany字段传递多个值?

我试过这个,但它不起作用。我主要对最后一行代码感兴趣。 如果我仅传递{'name': 'not test'}它可以正常工作,但是'songs'作为第二个参数会中断。

模型:

class Playlist(models.Model):
    name = models.CharField(max_length=50)
    songs = models.ManyToManyField("Song", blank=True)

试验:

def test_playlist_edit(self):
        """
        """
        playlist = Playlist.objects.create(name='test', num_of_songs=0,
            duration=0, owner=self.user)
        song = Song.objects.create(title='some song')
        response = self.client.post(reverse('playlist_edit',
            kwargs={'playlist_id': playlist.id}),
             {'name': 'not test', 'songs': [song]})

0 个答案:

没有答案