# how to add a field to User.groups.through in Django?

**URL:** <https://forum.djangoproject.com/t/how-to-add-a-field-to-user-groups-through-in-django/3928>\
**Category:** Using Django\
**Created:** [August 17, 2020, 11:44am UTC](https://forum.djangoproject.com/t/how-to-add-a-field-to-user-groups-through-in-django/3928 "2020-08-17T11:44:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rmaleki](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/rmaleki/32/1862_2.png) [@rmaleki](https://forum.djangoproject.com/u/rmaleki)\
**Post date:** [August 17, 2020, 11:44am UTC](https://forum.djangoproject.com/t/how-to-add-a-field-to-user-groups-through-in-django/3928/1 "2020-08-17T11:44:15Z")

</div>

I want to add my field to the table user\_user\_group. I can add a field to table groups but I can not add a field to the many-to-many group field in the user model. I use a monkey solution for add field to groups model but it does not work for many-to-many group field in the user model.

this code work:

```auto
Group.add_to_class('type', models.IntegerField(choices=RoleType.choices, default=RoleType.ADMIN))

```

this code does not work:

```auto
User.groups.through.add_to_class('organization', models.ForeignKey(Organization, on_delete=models.CASCADE, null=True))

```

are you have any solution?

solve this solution or tell me other solution

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 17, 2020, 1:16pm UTC](https://forum.djangoproject.com/t/how-to-add-a-field-to-user-groups-through-in-django/3928/2 "2020-08-17T13:16:17Z")

</div>

If I were faced with this type of situation, I would create my own User model that defines its relationship with Group with a ManyToManyField using the `through` parameter to specify my custom table.  
I also wouldn’t try to add fields directly to the Group table, I’d create a OneToOne relationship with additional attribute fields.

(I _really_ don’t like monkey-patches, especially when it comes to core. You never know when it’s going to come back to bite you.)

Ken

---

<div class="post-metadata">

**Author:** ![rmaleki](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/rmaleki/32/1862_2.png) [@rmaleki](https://forum.djangoproject.com/u/rmaleki)\
**Post date:** [August 19, 2020, 6:18am UTC](https://forum.djangoproject.com/t/how-to-add-a-field-to-user-groups-through-in-django/3928/3 "2020-08-19T06:18:00Z")

</div>

Thanks  
but this solution Complicates my code because I should override the Django permission manager. For example, overrides function get user groups and etc. Maybe I did not understand your solution. can Do you explain more this solution by code or another way?

---

<div class="post-metadata">

**Author:** ![rmaleki](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/rmaleki/32/1862_2.png) [@rmaleki](https://forum.djangoproject.com/u/rmaleki)\
**Post date:** [August 19, 2020, 6:25am UTC](https://forum.djangoproject.com/t/how-to-add-a-field-to-user-groups-through-in-django/3928/4 "2020-08-19T06:25:58Z")

</div>

is it your solution like this?

```
class User(AbstractUser):
    groups = models.ManyToManyField(
        Group,
        verbose_name=_('groups'),
        blank=True,
        help_text=_(
            'The groups this user belongs to. A user will get all permissions '
            'granted to each of their groups.'
        ),
        related_name="user_set",
        related_query_name="user",
        through="UserGroups"
    )

 class UserGroups(models.Model):
     user = models.ForeignKey(User, on_delete=models.CASCADE)
     group = models.ForeignKey(Group, on_delete=models.CASCADE)
     organization = models.ForeignKey(Organization, on_delete=models.CASCADE, null=True)
```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 19, 2020, 6:28am UTC](https://forum.djangoproject.com/t/how-to-add-a-field-to-user-groups-through-in-django/3928/5 "2020-08-19T06:28:45Z")

</div>

Yea, that’s the basic idea I was thinking of - I’m not immediately aware of any problems this would cause, but I could well be wrong.

Ken

---

<div class="post-metadata">

**Author:** ![rmaleki](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/rmaleki/32/1862_2.png) [@rmaleki](https://forum.djangoproject.com/u/rmaleki)\
**Post date:** [August 19, 2020, 6:34am UTC](https://forum.djangoproject.com/t/how-to-add-a-field-to-user-groups-through-in-django/3928/6 "2020-08-19T06:34:03Z")

</div>

Thanks,  
excellent. I will use this solution.
