[Django]-TypeError: Direct assignment to the forward side of a many-to-many set is prohibited. Use meeting.set() instead. Error with Django m2m fields

3👍

You can not use meeting=meeting_obj, since this is a ManyToManyField, and in order to add something to a ManyToManyField, first both objects need to have a primary key.

You can later add it to the ManyToManyField, for example with:

for email in participants_emails:
    participant_obj, created = Participant.objects.get_or_create(email=participants_emails) 
    participant_obj.meeting.add(meeting_obj)
    print(f'participant {participant_obj} was created==== ',created)

Likely you also want to use get_or_create(…) [Django-doc] since create(…) [Django-doc] does not return a 2-tuple with created.

Leave a comment