Bumfuzzled Relations

1 view
Skip to first unread message

gtaylor

unread,
Jun 9, 2006, 11:52:57 AM6/9/06
to Django users
Greetings,

I'm not sure if this is the best way to go about achieving my goal, so
please let me know if there is a better way. First a little background:

I'm working on a website with authentication, and at the time of user
creation I'd like the applicant to be able to select the school they go
to. It would appear that there's no sane way to add on to the User
model and add another profile field to the auth_user table and have it
fit nicely, so I created a new model, 'user_affil'. This looks like:

from django.db import models
from linuxsite.apps.schools.models import School
from django.contrib.auth.models import User

# Create your models here.
class user_affil(models.Model):
user = models.ForeignKey(User, edit_inline=models.TABULAR,
core=True)
school = models.OneToOneField(School, core=True)

So I'm relating each user to a school. The school model is as such:
class School(models.Model):
name = models.CharField("School Name", maxlength=50, unique=True)

So it's a reasonably simple relation. I want to be able to edit a
user's school from the admin interface, and I'd like a nice clean way
to do so under regular views. I suspect things are going to be fine
outside of admin land, but I'm running into a problem. Check out this
screenshot:

http://gis.clemson.edu/~gtaylor/shot.png

With any number of schools defined, I'm only seeing the "User" header
and some static text indicating the user's current school under the
standard Django 'Change User' admin interface. I'd like to have a
dropdown box there so I can select a different school.

Am I doing this completely wrong and is there a better way to do it?
I'm going to need to do some other similar relations down the road, so
figuring this out will allow for a lot of other things down the road
for my project.

Any feedback is appreciated. I've been looking through the
documentation, but the examples don't seem to cover this well.

gtaylor

unread,
Jun 9, 2006, 12:20:02 PM6/9/06
to Django users
I managed to get the field to show up properly on the admin interace by
making both fields ForeignKeys, but when I select a school in the
'Change User' page and hit 'Save and Continue Editing', I look down at
the field and it's blank again. The model's table is also empty. Any
idea on this?

oggie rob

unread,
Jun 10, 2006, 1:57:58 AM6/10/06
to Django users
As you found out, OneToOneField wouldn't work in this case. A OneToOne
field implies that there will be exactly one user per school - instead
you wanted several users to be affiliated with an individual school.

The answer to your question might be here:
http://www.b-list.org/weblog/2006/06/06/django-tips-extending-user-model

Seems a lot of people have been asking about this lately...

-rob

Reply all
Reply to author
Forward
0 new messages