Asserting for presence of added objects in Django ManyToMany relation

后端 未结 2 709
时光说笑
时光说笑 2021-01-27 04:43

I have the following model;

class Station(models.Model):
    name = models.CharField(max_length=50)
    address = models.TextField(default=\'\')
    owner = mode         


        
2条回答
  •  情歌与酒
    2021-01-27 05:09

    A few issues with your code, this is the output in my console:

    >>> station.members
    
    

    station.members is a ManyRelatedManager rather than a list of user2 and user3.

    station.members.all() will return you a list of user2 and user3, but station.members.all() is a QuerySet instead of a list:

    >>> type(station.members.all())
    
    

    So doing assert station.members.all() == [user2, user3] will never be True.

提交回复
热议问题