1

I have the following model;

class Station(models.Model):
    name = models.CharField(max_length=50)
    address = models.TextField(default='')
    owner = models.ForeignKey(User,default='')
    members = models.ManyToManyField(User, related_name='members')        

Now after the following code;

user1 = User.objects.create_user(username="username1",
                                     password="password1")
user1.save()
user2 = User.objects.create_user(username="username2",
                                     password="password2")
user2.save()
user3 = User.objects.create_user(username="username3",
                                     password="password3")
user3.save()
station = Station(name="somename",                            
                  address="someaddress",
                  owner=user1,
)
station.save()

station.members.add(user2,user3)

I want to assert that the added users are indeed there as the "members" of "Station"

Someone please tell me how

assert station.members == [user2,user3] doesn't fly. station.members actually is <Station: Station object>.members instead.

Afzal S.H.
  • 1,084
  • 2
  • 14
  • 27

2 Answers2

0

station.members is a Manager, i.e. it is the accessor for queries on the related users. You need to actually perform a query: in this case, station.members.all().

Mathieu Dhondt
  • 8,405
  • 5
  • 37
  • 58
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
0

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

>>> station.members
<django.db.models.fields.related.ManyRelatedManager object at 0x110774b10>

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())
<class 'django.db.models.query.QuerySet'>

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

Cheng
  • 16,824
  • 23
  • 74
  • 104
  • No but I can check `assert user2 in station.members.all()` and `assert user3 in station.members.all()` – Afzal S.H. May 13 '15 at 07:50
  • @afzal_SH I know you can do that. Just want to point out that '==' will not work. Hope this helps :) – Cheng May 13 '15 at 07:54
  • Actually I needed just to read Django's documentation on ManyToMany relation a little proper. I got the answer doing that just after I posted but by the time I got here Daniel Roseman had the answer :). In case you din notice I was originally trying to do `assert station.members == [user2, user3]` Not even `assert station.members.all() == [user2, user3]` :) – Afzal S.H. May 13 '15 at 08:15
  • 1
    @afzal_SH I am fully aware that you are doing station.members == instead of station.members.all() ==. But, it is quite obvious that you are going to do `assert station.members.all() == [user2, user3]` next. I have no intention to argue with you. As long as you get what you need great. – Cheng May 13 '15 at 08:18
  • @:D Okay! Wasn't really arguing though! – Afzal S.H. May 13 '15 at 17:01