Home > Blockchain >  'NoneType' object has no attribute 'add' (Django nested serializer)
'NoneType' object has no attribute 'add' (Django nested serializer)

Time:10-21

I'm trying to update object but

if main_photos_data.get('id', None):
            MainPhoto.objects.filter(id=main_photos_data.get(
                'id')).update(**main_photos_data)
        else:
            id = instance.id
            project = Project.objects.get(id=id)
            new_main_photo = MainPhoto.objects.create(**main_photos_data)
            *project.main_photo.add(new_main_photo)* error in this line

getting this error:

  File "/home/askar/work/tasnif-backend/portfolio/serializers.py", line 143, in update
    project.main_photo.add(new_main_photo)
AttributeError: 'NoneType' object has no attribute 'add'

I've tried:

project.main_photo = new_main_photo

But got this error:

TypeError at /portfolio/edit-project/1/
Direct assignment to the forward side of a many-to-many set is prohibited. Use photo.set() instead.

CodePudding user response:

try this

project.main_photo.id = new_main_photo.id
  • Related