1

Hi I am working on django permissions system with Django 2+ . And I want to add extra fields inside the Group model in Django but right now I have no idea how to do that. I tried something like:

models.py
from django.contrib.auth.models import Group

class Group(models.Model):
    Group.add_to_class('description', models.CharField(max_length=180,null=True, blank=True))

But when I migrate my model then it throwing error as:

Migrations for 'auth':
  /usr/local/lib/python3.6/site-packages/django/contrib/auth/migrations/0010_group_description.py
    - Add field description to group
Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 316, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 353, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/makemigrations.py", line 184, in handle
    self.write_migration_files(changes)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/makemigrations.py", line 223, in write_migration_files
    with open(writer.path, "w", encoding='utf-8') as fh:
PermissionError: [Errno 13] Permission denied: '/usr/local/lib/python3.6/site-packages/django/contrib/auth/migrations/0010_group_description.py'  
Amitjoc
  • 83
  • 4
  • 9

2 Answers2

4

In order to modify an existing model you have to inherit from the model:

from django.contrib.auth.models import Group


class CustomGroup(Group):
    description = models.CharField(max_length=180,null=True, blank=True)
    

Using the add_to_class means relying on undocumented internals which have no stability guarantee, and is subject to change without warning.

Mohit Harshan
  • 1,916
  • 1
  • 18
  • 41
  • Hi Mohit thanks for your answer but it is not possible to modify the existing model (Group) instead of create new CustomGroup model – Amitjoc Sep 04 '20 at 06:30
  • 1
    i think it will be a hack and subject to consequences . Extending the group model is same as extending the AbstractBaseUser model and is clear from the docuumentation . also using add_to_class only adds to meta properties of the model . – Mohit Harshan Sep 04 '20 at 08:26
1

Try this, (Not in the Group class)

# models.py
from django.contrib.auth.models import Group

Group.add_to_class('description', models.CharField(max_length=180,null=True, blank=True))

BATMAN
  • 375
  • 2
  • 14
  • Hi Thanks for your support but still this is not working for me. I tried and I am getting the same error. – Amitjoc Sep 04 '20 at 06:13
  • 1
    This is the right way to do it, the problem is that the migration file is being created inside your django installation (as auth is a django module) where you don't have write permission. I strongly suggest using a virtual environment for installing django, that way this is possible. – vlizana Oct 25 '21 at 19:40
  • 1
    While this works, this is a monkey patch in a way. What happens when a new person installs a fresh virtual environment. What happens when the packages are updated? Using a custom model will save you from future headaches. – dakdad Mar 22 '23 at 04:06
  • Agreed, but since the OP wanted to update the existing Group class, this came to mind. Extending and using the custom class is the way to go for sure. – BATMAN May 01 '23 at 15:47