Adding Model Inline with Group Model

194 views
Skip to first unread message

VoiDeT

unread,
Dec 3, 2008, 2:29:08 PM12/3/08
to Django users
Hey there,

I am trying to figure out how to solve this:
I have created a new model called GroupLevels to extend the already
existing Group model by django.contrib.auth.models.group. I wish to
have the GroupLevels inline with the Group Model in the admin panel. I
have attempted to do this via:

class GroupLevels(models.Model):
group = models.ForeignKey(Group, unique=True)
shows = models.PositiveIntegerField(default=0)
items = models.PositiveIntegerField(default=0)

class Meta:
verbose_name_plural = "Group Permissions"
verbose_name = "Group Permission"

def __unicode__(self):
return self.group.name

class GroupInline(admin.TabularInline):
model = Group

class GroupAdmin(admin.ModelAdmin):
inlines = [GroupInline,]

admin.site.register(Group, GroupAdmin)

However i get an error saying that there is no foreign key from Group
to GroupLevels. What am i doing wrong here? How do i get them to
display inline in the admin?

Thanks alot for any advice!

Karen Tracey

unread,
Dec 3, 2008, 2:38:04 PM12/3/08
to django...@googlegroups.com
On Wed, Dec 3, 2008 at 2:29 PM, VoiDeT <voi...@gmail.com> wrote:

Hey there,

I am trying to figure out how to solve this:
I have created a new model called GroupLevels to extend the already
existing Group model by django.contrib.auth.models.group. I wish to
have the GroupLevels inline with the Group Model in the admin panel. I
have attempted to do this via:

class GroupLevels(models.Model):
       group = models.ForeignKey(Group, unique=True)
       shows = models.PositiveIntegerField(default=0)
       items = models.PositiveIntegerField(default=0)

       class Meta:
               verbose_name_plural = "Group Permissions"
               verbose_name = "Group Permission"

       def __unicode__(self):
               return self.group.name

class GroupInline(admin.TabularInline):
       model = Group

I think you want to specify the model as GroupLevels -- isn't that what you want to be inline?
 

class GroupAdmin(admin.ModelAdmin):
       inlines = [GroupInline,]

admin.site.register(Group, GroupAdmin)

However i get an error saying that there is no foreign key from Group
to GroupLevels. What am i doing wrong here? How do i get them to
display inline in the admin?

I don't see GroupLevels mentioned at all in what you posted for the admin, so I'm a bit confused by the error message you are reporting.  But it sounds like you have things specified backwards in terms of what's inline with what. 

Karen

VoiDeT

unread,
Dec 3, 2008, 2:46:14 PM12/3/08
to Django users
Hi Karen,

Thanks alot for your quick reply!
Indeed the admin.site.register(Group, GroupAdmin) was meant to write
as admin.site.register(GroupLevels, GroupAdmin). However when i do
this it doesn't display it inline, instead merely throws back an error
saying Group doesn't have a foreignkey to GroupLevels.

So what i have above is trying to link GroupLevels inline with the
Group model, or no?

What am i missing here?
Thanks alot

Karen Tracey

unread,
Dec 3, 2008, 6:49:01 PM12/3/08
to django...@googlegroups.com

I think you've got things backwards in terms of what should be specified as Inline and what should be registered.  You're trying to inline Groups in GroupLevels, but your models as specified support inlining GroupLevels in Groups:

class GroupLevelsInline(admin.TabularInline):
       model = GroupLevels

class GroupAdmin(admin.ModelAdmin):
       inlines = [GroupLevelsInline,]

admin.site.register(Group, GroupAdmin)

Karen
Message has been deleted

VoiDeT

unread,
Dec 3, 2008, 7:08:53 PM12/3/08
to Django users
Hey Karen,

I have tried this scenario above, however the GroupLevels does not
show inline with the Groups Model in Admin. I don't understand why
not, no errors, just simply does not display. I have also tried to
inline the Groups Model into the GroupLevels Model yet i get an error
saying that Groups doesn't have a foreignkey to GroupLevels which is
true.

So what am i missing here?

This is my code now:

class GroupLevels(models.Model):
group = models.ForeignKey(Group, unique=True)
shows = models.PositiveIntegerField(default=0)
items = models.PositiveIntegerField(default=0)

class Meta:
verbose_name_plural = "Group Permissions"
verbose_name = "Group Permission"

def __unicode__(self):
return self.group.name

class GroupLevelsInline(admin.TabularInline):
model = GroupLevels

class GroupLevelsAdmin(admin.ModelAdmin):
inlines = [GroupLevelsInline,]

admin.site.register(Group, GroupLevelsAdmin)

Karen Tracey

unread,
Dec 3, 2008, 7:43:05 PM12/3/08
to django...@googlegroups.com

Since you are overriding an existing admin definition, you'll need to admin.site.unregister(Group) before you can register your own. Not sure why you are not seeing an AlreadyRegistered exception -- are you including the admin defs in your models.py file?  You should put them in an admin.py file and use admin.autodiscover() in your urls.py.  If you do that with what you have now I think you will see AlreadyRegistered when you attempt to navigate to the admin url, which you can fix by first unregistering the existing Group admin before registering your own.

Karen

VoiDeT

unread,
Dec 3, 2008, 8:04:39 PM12/3/08
to Django users
Excellent!
That worked fine, i guess i had to unregister it first and then dump
my code into the admin.py file.
However now the Group is a multiple select widget whereas before it
would use a widget that i could add group permissions and take them
away via an arrow to two neighbouring multiple select boxes. I'm
looking at the models.py file in django.contrib.auth.models and wonder
how i would go about bringing back this widget?

Thanks for all your help Karen

Karen Tracey

unread,
Dec 3, 2008, 8:42:05 PM12/3/08
to django...@googlegroups.com

Look in django/contrib/auth/admin.py to see what the original Group admin definitions are.

Karen

VoiDeT

unread,
Dec 3, 2008, 8:54:36 PM12/3/08
to Django users
Perfect!

Thanks alot Karen :)
Just getting my bearings into the plentiful overriding :D
Reply all
Reply to author
Forward
0 new messages