Question

Hello everyone and thanks for reading.

I have a simple problem that I want to get rid of and I have not seen examples where this is achieved yet although searching around the net for quite a while.

I recently extended with UserProfile so my admin.py looks like this:

from django.contrib import admin
from django.contrib.auth.models import User
from models import UserProfile
from django.contrib.auth.admin import UserAdmin

admin.site.unregister(User)

class UserProfileInline(admin.StackedInline):
                model = UserProfile

class UserProfileAdmin(UserAdmin):
#               fieldsets = [
#                       (None,  {'fields': ['image']}),
#                       ('Avatar', {'fields': ['text'], 'classes': ['collapse']}),
#               ]
                inlines = (UserProfileInline,)

admin.site.register(User, UserProfileAdmin)

and models.py like this:

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

class UserProfile(models.Model):
        # Required
        user = models.ForeignKey(User, unique=True)
        image = models.ImageField(upload_to='media/users/', blank=True, help_text="Your face")
        text = models.TextField(blank=True, help_text="Write something about yourself")

In an app called users that is referred to by settings.py with:

    AUTH_PROFILE_MODULE = users.UserProfile

1

Basically what I want to achieve is to get rid of the #1 StackedInLine that shows in the admin. The reason I use StackedInLine instead of TabularInLine is because otherwise I get a "Delete?" column to the right and I find it optional so I would like to either exclude that or get rid of the #1 numbering in StackedInLine.

This is what it looks like in "the real world" .. (Marked with red)

Stacked: hxxp://i28.tinypic.com/2lsadkg.jpg

Tabular: hxxp://i27.tinypic.com/4lybs.jpg

2

Also. I wonder why I cannot use fieldsets when I have loaded the UserProfile models.py file in admin.py. It simply says the field doesn't exist. Do I have to call the fields differently than in django/ contrib/auth/admin.py where I've seen it work?

If you feel like there is a more efficient way of doing this just tell me. I am open for all answers.

Was it helpful?

Solution

1

I think you're getting too picky here. If you absolutely need control over such minute details you should create your own views instead of using the admin. Otherwise stacked is what you want because tabular doesn't make much sense for one-to-one relations.

2

I've been able to use fieldsets in user profiles. The only difference between my code and yours seems to be that I'm using tuple's instead of dict's. Here's mine for comparison:

class UserProfileInline(admin.StackedInline):
    model = UserProfile

class MyUserAdmin(UserAdmin):
    inlines = [UserProfileInline]
    fieldsets = (
        (None, {'fields': ('username', 'password')}),
        (_('Personal info'), {'fields': ('first_name', 'last_name', 'email')}),
        (_('Permissions'), {'fields': ('is_staff', 'is_active', 'is_superuser')}),
        (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
        (_('Groups'), {'fields': ('groups',)}),
    )
    exclude = ['user_permissions']

EDIT:

I just did a quick check and the "#1" is coming from the admin template.

This means you can easily remove it by overriding the stock admin template, although this will affect all your inlines including ones that are one-to-many.

The stacked inline template can be found in django/contrib/admin/templates/admin/edit_inline/stacked.html

This means you can copy the template to your own templates directory as templates/admin/edit_inline/stacked.html and this will be loaded by Django at run time instead of the stock template.

After copying edit your local copy to remove #{{ forloop.counter }} on line 9.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top