mercredi 6 mai 2015

Django : Check object existance in ManytoManyField

I have ChatRoom model as,

class ChatRoom(models.Model):
    name = models.CharField(max_length=300)
    owner = models.ForeignKey(UserProfile)
    description = models.CharField(max_length=300)
    members = models.ManyToManyField(UserProfile,related_name='members')
    members_requested = models.ManyToManyField(UserProfile,related_name='member_requested')
    members_blocked = models.ManyToManyField(UserProfile,related_name='member_blocked')
    def __unicode__(self):
        return self.name

For example I have added a UserProfile instances to members field.

chr_obj.members.add(up_obj1,up_obj2)

How can i check whether a UserProfile object is there or not. I tried with, if chr_obj.name ='new room'

up_obj.chatroom_set.filter(name='new room').exists()

but it returns false. How to verify that ?? Thanks.

Aucun commentaire:

Enregistrer un commentaire