Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django ManyToMany FIeld: 'tuple' object has no attribute 'user'

Having a little django problem i'm stuck with... My Model:

class Mymodel(models.Model):
    [...]
    user = models.ManyToManyField(User)

My attempt to create a new user on it

mymodel = Mymodel.objects.get_or_create(date=date, day=day, time=time) # This one gives a solid Mymodel object i can play with
mymodel.user.add(user) # User is a instance of the Django User System

When trying to execute, it throws 'tuple' object has no attribute 'user'

Did i accidentally turn it into a tuple?

like image 489
patchrail Avatar asked Dec 22 '22 07:12

patchrail


1 Answers

get_or_create returns a tuple consisting of (object, created). To get just the model, use:

mymodel, _ = Mymodel.objects.get_or_create(date=date, day=day, time=time)

or

mymodel = Mymodel.objects.get_or_create(date=date, day=day, time=time)[0]
like image 175
mipadi Avatar answered May 14 '23 03:05

mipadi