Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django admin - stackedInline single instance

I'm building a site based on a highly customized django admin instance and am running into issues with user profiles as an inline to user_admin

long story short regardless of what I set for max_num and extra in the admin.StackedInline instance it allows up to 2 profiles per user - with a blank one in place by default if the user has an existing profile

anyone know how I could adjust this to show only a single inline profile without resorting to some JS front end hack?

relevant code from: profiles.admin.py

from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin
from profile.models import user_profile

class user_profile_admin(admin.StackedInline):
    model = user_profile

    fk_name = 'user'
    max_num = 1
    extra = 0

class user_admin_extended(UserAdmin):
    inlines = [user_profile_admin, ]

admin.site.unregister(User)
admin.site.register(User, user_admin_extended)
like image 958
Alvin Avatar asked Mar 05 '10 01:03

Alvin


1 Answers

I assume you're using FK field to connect user and profile? Try OneToOneField it should render just one inline in admin.

like image 98
Dmitry Shevchenko Avatar answered Oct 26 '22 19:10

Dmitry Shevchenko