6

I'm trying to add a field to 'Groups' within the Django admin - for instance, when you create a group in the backend, you define 'Name' and 'Permissions', and I'd like to add a field to that list (CharField). Does this require a new app, or can I extend the Group model in my root models.py?

New to django and python here, so sorry if the question is badly worded.

Here's what I have so far:

#models.py

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

class AppDBName(Group):
    AppDB = models.CharField(max_length=30)
    def __unicode__(self):
        return self.AppDB

#admin.py

from django.contrib import admin
from .models import AppDBName   

admin.site.register(AppDBName)
fireinspace
  • 197
  • 1
  • 1
  • 8

2 Answers2

12

You can try to create a new model, which will extend the Django built-in group model. To do this you should link a new model, say GroupExtend, to the original one with a OneToOne field, like it can be done for user (link):

from django.contrib.auth.models import Group
    
class GroupExtend(models.Model):
    group = models.OneToOneField(Group, on_delete=models.CASCADE)
    # other fields
Kevin Languasco
  • 2,318
  • 1
  • 14
  • 20
chem1st
  • 1,624
  • 1
  • 16
  • 22
  • how would i pull that into the admin? would it be: from .models import GroupExtend admin.site.register(GroupExtend) – fireinspace Oct 11 '15 at 19:55
  • 2
    Since a new group you'll create will be an instance of both `Group` and `GroupExtend` you can either register a `GroupExtend` (then you'll need to define the related `Group` manually) or set it as an inline to `Group` model and re-register the latter as it is shown in docs (I've already posted a link). – chem1st Oct 11 '15 at 21:07
  • 5
    But here, for instance to create a "group" I would need to first make an instance of the Group model and then pass that instance in the GroupExtend model. Isn't it kinda more messy? I mean it seems that directly inheriting from Group model should be more sensible. Can you please explain why this method of creating another model and then joining that to Group with an OneToOneField should be the way? It will be really helpful. – fpaekoaij Jan 09 '20 at 17:03
-4

You don't need to create new app just override the django admin models.py in to your project and customize it as per your requirement.

https://docs.djangoproject.com/en/1.8/topics/db/models/

Ishwar
  • 1
  • 3
  • thanks. i added the class to models.py and registered in admin.py, but still not having any luck. added code above for reference. – fireinspace Oct 11 '15 at 18:54