The argument TypeError: add () after * must be a sequence, not subscribers

class Subscribers(User): date = models.DateField() user = models.OneToOneField(User) class Tour(models.Model): owner_id = models.ForeignKey(User) name = models.CharField(max_length=50) location = models.ManyToManyField(Location) subscribers = models.ManyToManyField(Subscribers, related_name="sub") 

I am trying to do this in another file:

 user1 = User.objects.create_user('John',' j@j.com ','j1') user2= User.objects.create_user('Mike',' m@m.com ','m1') user3= User.objects.create_user('kokoko',' m@m.com ','m1') user4= User.objects.create_user('lalal',' m@m.com ','m1') sub = Subscribers() tour = Tour() tour.id = "1" tour.name = "hello" tour.owner_id = user1 tour.subscribers = sub 

but I have this error: Argument typeError: add () after * should be a sequence, not subscribers

+4
source share
1 answer

ManyToMany Manager assumes that when you do

 tour.subscribers = sub 

sub is a sequence (tuple, list, request) of Subscribers , and not a single object. Then this is the same as doing:

 tour.subscribers.add(*sub) 

And since sub is not a sequence, it throws such an error. I would recommend saving and adding first later. I think this is also more readable, but it can only be my opinion:

 sub = Subscribers() tour = Tour() tour.id = "1" tour.name = "hello" tour.owner_id = user1 tour.save() tour.subscribers.add(sub) 

Hope this helps :)

+1
source

All Articles