Adding info in django many-to-many table

I created many-to-many connection in models.py between two tables and the third one appeared. I've stuck into the problem that i dont know how to add some info to this table.

I've been trying to import this table in shell mode but i dont know how to do it. For instance, to change table based on class 'User' i wrote:

from club.models import User
new = User(name='Maksym')
new.save()

So i'm interested in how to add some info in the table 'club_event1_users', i have attached screenshotsenter image description here

enter image description here

I created many-to-many connection in models.py between two tables and the third one appeared.

This is expected behavior. Django creates a many-to-many field by defining a junction table [wiki], with two ForeignKeys to the two models.

I've stuck into the problem that I dont know how to add some info to this table.

Django has a section titled Many-to-many relationships that explains how you can retrieve, and update the many-to-many table. Indeed, you can for example create a User, an Event1, and add that Event1 to the User:

my_user = User.objects.create(name='User name')
my_event = Event.objects.create(
    name='My fancy event',
    event_date='2022-09-11',
    description='my fancy event description',
)

my_event.users.add(my_user)  # add user to the event

You can also access the model with:

Event1User = Event1.users.through  # model for the many-to-many model
Back to Top