0

In the models.py file of the Django auth app, there are models for user and group which create their respective tables in the database:

  • class user -> auth_user
  • class group -> auth_group

There is no class user_groups, and yet something fancy happens behind the scenes to also create a table auth_user_groups which tracks which users are in which groups. (Each row contains a single user_id and group_id).

Can someone help me understand how this happens? I may want to replicate this functionality in my own app, where I associate customer models with a group_id.

dxt
  • 122
  • 1
  • 3
  • 13
Dustin Michels
  • 2,951
  • 2
  • 19
  • 31

1 Answers1

0

User and Groups share a ManyToMany relationship.

See: https://docs.djangoproject.com/en/2.1/topics/db/examples/many_to_many/

groups

Many-to-many relationship to Group

https://docs.djangoproject.com/en/2.1/ref/contrib/auth/#django.contrib.auth.models.User.groups

Risadinha
  • 16,058
  • 2
  • 88
  • 91