A
A
authoraytee2021-06-24 01:53:20
Django
authoraytee, 2021-06-24 01:53:20

How to save ManyToMany model in django ORM?

It is not possible to save data to a table with a many-to-many relationship. I save

it like this:

new_subscriber = EventToSubscriber(event=event_id, subscriber=current_user)
    new_subscriber.save()


Model:
class EventToSubscriber(Model):
    event = models.ManyToManyField(Event, verbose_name='Событие')
    subscriber = models.ManyToManyField(User, verbose_name='Подписчик')


Gives me:
TypeError at /subscribe/4
Direct assignment to the forward side of a many-to-many set is prohibited. Use event.set() instead.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stefan, 2021-06-24
@authoraytee

new_subscriber = EventToSubscriber.objects.create()
new_subscriber.event.add(event_id)
new_subscriber.subscriber.add(current_user)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question