views:

410

answers:

1

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.

+2  A: 

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.

Van Gale
Yes I was thinking that myself, but if you need to make a website for someone it doesn't hurt it looks different than all the others.I am also able to use fieldsets of course, but I cannot access the additional fields like image or text. It will tell me it doesn't exist. Do you have to do a getprofile thing for these to work in fieldsets also?So removing the #1 is not possible?
dezza
Edited my answer to show how to remove the #1
Van Gale
Your problem with the user profile fields is just what it says: the user profile object does not exist. This means you always need to create a matching profile whenever a user object is created. See the django-annoying project for an "AutoOneToOneField" that will do this for you: http://bitbucket.org/offline/django-annoying/wiki/Home
Van Gale
Is it not possible to do this manually by inserting the OneToOne field yourself instead of using another Django addon?
dezza
Thanks alot ! I owe you one :) ..
dezza
@Van +1 I always find your answers very helpful.
orokusaki