Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'ManyToManyDescriptor' object has no attribute 'add', why?

Tags:

python

django

So I refer to another model :

 subscriptions = models.ManyToManyField(Season)

So I use :

@api_view(['POST'])
def buy_season(request):
    _id = 1
    season = Season.objects.get(id = _id)

    a = ExtUser.subscriptions.add(season)

    a.save()

    return Response({'status': 'success'}, status=status.HTTP_200_OK)

I get an error object 'ManyToManyDescriptor' does not attribute the "Add" Do many to many directly , and not through the " throw " , why there is this error ?

like image 684
Alexandr Domoryonok Avatar asked Jun 18 '16 22:06

Alexandr Domoryonok


2 Answers

You should use an instance of the ExtUser model as you cannot add a model object directly to a class object.

like image 75
Moses Koledoye Avatar answered Oct 24 '22 15:10

Moses Koledoye


User instance instead of class.

a = ExtUser.objects.get(id=user_id).subscriptions.add(season)

You need to update the above query and it will fix the issue.

like image 36
Usman Maqbool Avatar answered Oct 24 '22 16:10

Usman Maqbool