9

How add a new field in Group Model of the Django? To use something like

from django.contrib.auth.models import Group
g = Group.objects.get(pk=1)
g.extra_field = "something"
g.save()

it's posssible?

EDIT:

I need a persistent extra field.

Luiz Carvalho
  • 1,549
  • 1
  • 23
  • 46

2 Answers2

11

This isn't in the documentation but fine to use (pre to have a basic understanding of monkey patching), in your models.py or init add:

from django.contrib.auth.models import Group

Group.add_to_class('foo', bar)

Where bar can be any python object (or method), e.g.

def bar(self):
    return self.attr * 2

or using a field mapping:

Group.add_to_class('foo', models.RegexField(r'^hello$'))
Hedde van der Heide
  • 21,841
  • 13
  • 71
  • 100
2

I had the same problem. I wanted to add an extra field in Groups. It worked for me - Monkey patching

Sankalp
  • 1,182
  • 2
  • 17
  • 22