0

I am extending the Django User model to include a foreign key pointing at another model like so (just like it says in the Django docs):

models.py:

class Ward(models.Model):
    name = models.CharField(max_length=100)

    def __unicode__(self):
        return self.name

# Extending the user model
class WardMember(models.Model):
    user = models.OneToOneField(User)
    ward = models.ForeignKey(Ward)

    def __unicode__(self):
        return self.ward.name

admin.py:

class WardMemberInline(admin.StackedInline):
    model = WardMember
    can_delete = False
    verbose_name_plural = 'ward member'

# Define a new User admin
class UserAdmin(UserAdmin):
    inlines = (WardMemberInline, )

admin.site.register(Ward)

# Re-register UserAdmin to get WardMember customizations
admin.site.unregister(User)
admin.site.register(User, UserAdmin)

When I create a new user in the admin interface I want this new WardMember.ward extension to be required. Currently it's not enforcing that. Here's what happens:

  1. Create user succeeds without a ward
  2. Create other records as user succeed
  3. Edit user now won't let me save unless there is a ward selected

I'd really like #1 above to fail.

I've tried figuring out how to override save() for User using a proxy object but that's not working. I looked into the pre_save signal but the docs explicitly say that's not for vetoing saves.

What is the right approach?

Additional information:

  • I'm using 1.4. I see that in 1.5 I can extend the user class but I'm not in a position to update to 1.5 just yet.

I ended up forging ahead with Django 1.5, but I'll leave this here in case someone has a final answer to contribute that works with 1.4.

devguydavid
  • 3,917
  • 1
  • 19
  • 18
  • Why don't you extend `User` base model and add a foreign key to ward? That may do. – Paulo Bu Jun 03 '13 at 01:31
  • I see that I can do that with Django 1.5, but I'm on 1.4. Maybe I'll look into migrating, but I just have this one little thing to work out. (Question updated to include pertinent information) – devguydavid Jun 03 '13 at 01:33

1 Answers1

0

In django 1.3.1 I use this code and works fine:

from django.contrib.auth.models import User

class FilterSearchQueries(models.Model):
    title = models.CharField(max_length=250)
    owner = models.ForeignKey(User)
    place = models.CharField(max_length=250)
    query = models.TextField()
mrash
  • 883
  • 7
  • 18