Extending the User model

39 views
Skip to first unread message

Patricio Palma

unread,
Feb 8, 2009, 3:06:32 PM2/8/09
to Django users
Greetings

I have a model

from django.contrib.auth.models import User
class MyUser(User):

chilean_rut = CLRutField(_('RUT'),primary_key=True)

class Meta:

admin = meta.Admin(
fields = (
(_('Personal info'), {'fields': ('first_name', 'last_name',
'chilean_rut','email')}),
(_('Extended info'), {'fields': ('gamename', 'url',
'description')}),
(_('Permissions'), {'fields': ('is_staff', 'is_active',
'is_superuser', 'user_permissions')}),
(_('Important dates'), {'fields': ('last_login',
'date_joined')}),
(_('Groups'), {'fields': ('groups',)}),
),
list_display = ('username', 'gamename', 'first_name',
'last_name', 'is_staff'),

looks fine, but in my users list after added, I see only the total of
added users

just like list_display was empty


Thanks

Alex Gaynor

unread,
Feb 8, 2009, 3:13:20 PM2/8/09
to django...@googlegroups.com
This code looks exceptionally old, admin = meta.Admin() has been gone for several years, what version of django are you workign with?

--
"I disapprove of what you say, but I will defend to the death your right to say it." --Voltaire
"The people's good is the highest law."--Cicero

Patricio Palma

unread,
Feb 8, 2009, 3:16:56 PM2/8/09
to Django users
Greetings

I've a model
class MyUser(auth.User):
location = meta.CharField(maxlength=100, blank=True)

class META:
replaces_module = 'auth.users'
admin = meta.Admin(
list_display = ('username', 'email',
'first_name','last_name', 'lacation')
)


looks fine, but, after add a new myUser (successfully) my user list
it's empty

just like a list_display = ('')

what is wrong?

Thanks

Patricio Palma

unread,
Feb 8, 2009, 4:06:42 PM2/8/09
to Django users

> This code looks exceptionally old, admin = meta.Admin() has been gone for
> several years, what version of django are you workign with?
>

1.1 pre-alpha
I 've in admin.py file, the main idea was show the list_display field
setted

Daniel Roseman

unread,
Feb 8, 2009, 5:27:35 PM2/8/09
to Django users
That's impossible. This code is from version 0.90, maybe even older.
It just wouldn't work on 1.1.
--
DR.

Patricio Palma

unread,
Feb 8, 2009, 5:33:30 PM2/8/09
to Django users
At revision 9820.

???

Karen Tracey

unread,
Feb 8, 2009, 5:48:22 PM2/8/09
to django...@googlegroups.com
On Sun, Feb 8, 2009 at 5:33 PM, Patricio Palma <patric...@ic.uach.cl> wrote:

At revision 9820.

???

??? indeed.  What doc are you reading that suggests having  something like "admin = meta.Admin(..." under class Meta of your model definition?  I've never seen anything like it but I've only been around since 0.96 or so and others seem to recognize it as very old syntax from earlier releases.  If you were running r9820 I'd expect you to get a NameError on that line since there's nothing named meta to support that call.

The current way to set list_display, if you are running 1.0 or more recent, is documented here:

http://docs.djangoproject.com/en/dev/ref/contrib/admin/#modeladmin-options

The admin defs are all now included in ModelAdmin classes, there is no more admin definition tied into the model class definition itself.  But if you were running a relatively recent Django, I'd expect you'd be seeing errors with the code you are showing us, so I am rather confused by your report of no errors, just not the results you were expecting.

Karen

Patricio Palma

unread,
Feb 8, 2009, 7:51:35 PM2/8/09
to Django users
Yeah you're right
I can't believe that
I mix the admin settings in the class, for a short explanation.

my code:
-----------------------------------------------------------
models.py
from django.contrib.auth.models import User
class MyUser(User):
chilean_rut = CLRutField(_('RUT'),primary_key=True)
some_field = models.CharField(max_length=50)
------------------------------------------------------------
admin.py
class MyUserAdmin(admin.ModelAdmin):

list_display = ('chilean_rut', 'email','some_field')

admin.site.register(MyUser,MyUserAdmin)

-------------------------------------------------------------

And my list_display is EMPTY ( on the http://127.0.0.1:8000/admin/myapp/myuser/
)

Karen Tracey

unread,
Feb 9, 2009, 10:13:39 AM2/9/09
to django...@googlegroups.com
On Sun, Feb 8, 2009 at 7:51 PM, Patricio Palma <patric...@ic.uach.cl> wrote:
[snip]

-----------------------------------------------------------
models.py
from django.contrib.auth.models import User
class MyUser(User):
   chilean_rut  = CLRutField(_('RUT'),primary_key=True)
   some_field = models.CharField(max_length=50)
------------------------------------------------------------
admin.py
class MyUserAdmin(admin.ModelAdmin):
   list_display = ('chilean_rut', 'email','some_field')
admin.site.register(MyUser,MyUserAdmin)

-------------------------------------------------------------

And my list_display is EMPTY ( on the  http://127.0.0.1:8000/admin/myapp/myuser/)

The problem here seems to be caused by setting primary_key=True on one of the fields in the model that inherits from another.  You can see it using just these simple models:

class BaseM(models.Model):
  base_name = models.CharField(max_length=100)
  def __unicode__(self):
    return self.base_name

class DerivedM(BaseM):
  customPK = models.IntegerField(primary_key=True)
  derived_name = models.CharField(max_length=100)
  def __unicode__(self):
    return "PK = %d, base_name = %s, derived_name = %s" % \
        (self.customPK, self.base_name, self.derived_name)
 
and manage.py shell (output reformatted for readability):

Python 2.5.1 (r251:54863, Jul 31 2008, 23:17:40)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from ttt.models import DerivedM
>>> from django.db import connection
>>> DerivedM.objects.create(customPK=44, base_name="b1", derived_name="d1")
<DerivedM: PK = 44, base_name = b1, derived_name = d1>
>>> DerivedM.objects.all()
[]
>>> connection.queries[-1]
{'time': '0.006',
'sql': u'SELECT
          `ttt_basem`.`id`,
          `ttt_basem`.`base_name`,
          `ttt_derivedm`.`basem_ptr_id`,
          `ttt_derivedm`.`customPK`,
          `ttt_derivedm`.`derived_name`
       FROM `ttt_derivedm` INNER JOIN `ttt_basem`
       ON (`ttt_derivedm`.`customPK` = `ttt_basem`.`id`) LIMIT 21'}
>>>

The ORM is joining the two tables and attempting to match the manually-specified custom PK from the child table to the auto-generated id in the parent table, which results in finding nothing.  I do not know if specifying primary_key=True on a field in a child table is just not allowed (in which case it should probably be flagged as an error during validation) or if the ORM should be handling this properly.  I do not see any ticket open that describes this case; you might want to open one since it seems something ought to be changed here to either report an invalid model definition or handle this case properly.

Karen

Patricio Palma

unread,
Feb 9, 2009, 1:03:41 PM2/9/09
to Django users

Thanks, I owe you a chocolate bar

I fix it

class MyUser(User):
chilean_rut = CLRutField(_('RUT'),unique=True)
some_field = models.CharField(max_length=50)

voalá
I don't really need a PK, my mistake
Thanks again.

Malcolm Tredinnick

unread,
Feb 9, 2009, 9:31:13 PM2/9/09
to django...@googlegroups.com
On Sun, 2009-02-08 at 12:16 -0800, Patricio Palma wrote:
> Greetings
>
> I've a model
> class MyUser(auth.User):
> location = meta.CharField(maxlength=100, blank=True)
>
> class META:
> replaces_module = 'auth.users'
> admin = meta.Admin(
> list_display = ('username', 'email',
> 'first_name','last_name', 'lacation')
> )

Django hasn't used that syntax since the 0.91 release. Since then, admin
has been through two major revisions.

What version of Django are you using (keeping in mind that if you are
using 0.91, you're going to be pretty much on your own with solving most
problems, since it's from so long and so many changes ago)?

If you're using, say, Django 1.0, the documentation is your friend as to
the right syntax for the admin interface.

Regards,
Malcolm


Reply all
Reply to author
Forward
0 new messages