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:
TypeError: add() argument after * must be a sequence, not Subscribers
The ManyToMany manager assumes that when you do
subis a sequence (tuple, list, queryset) ofSubscribers, not a single object. Then doing so is the exact same as doing:And since sub is not a sequence, it throws such error. I would recommend saving first and adding later. I think it’s also more readable, but it may be just my opinion:
Hope this helps 🙂