1

Up until recently, a project I'm working used one mega UserProfile to handle all profile data for two different types of users. Naturally this was messy, and it was about time to refactor it.

In my attempt to refactor the model, I split the model into Requester and Funder and created an abstract UserProfile model which both subclass:

class UserProfile(models.Model):
  class Meta:
    abstract = True

  user = models.OneToOneField(User)

  def __unicode__(self):
      return unicode(self.user)


class Requester(UserProfile):
  def requested(self, event):
    """Check if a user requested an event."""
    return self == event.requester


class Funder(UserProfile):
  osa_email = models.EmailField(null=True) # The e-mail of the contact in OSA
  mission_statement = models.TextField(max_length=256)

And in my settings.py file, I adjusted the AUTH_PROFILE_MODULE.

AUTH_PROFILE_MODULE = "app.UserProfile"

The problem is, when hitting a page that uses "User.get_profile()" it breaks, reporting:

Unable to load the profile model, check AUTH_PROFILE_MODULE in your project settings

I'm not quite sure what's going on here. According to the docs, everything looks right.

Can some explain why this fails? (There are a bunch of alternative solutions I've come across, but I'd much prefer to fix this if possible than adopt some hack.)

4

1 回答 1

2

What you are trying to do it not possible. AUTH_PROFILE_MODULE is expecting a concrete model, not an abstract one. Concrete means it has a table and can create instances. An abstract model can only be subclassed.

A logic reason why this not possible it that django has no one of knowing which model instance to return for your user. A Requester? A Funder? Simply being an abstract reference gives django no hints. One approach might be to look into the contenttypes framework and maybe come up with a generic UserProfile model containing a reference to the proper sub-profile type. You could then remove the abstract=True from your UserProfile, and create a generic relation to the specific Profile model. AUTH_PROFILE_MODULE would then simply reference that single UserProfile, but its instances can then use the .content_object to get the specific subobject.

There are many ways I'm sure you could address this problem, but I am just commenting on the reason why this specific approach does not work.

于 2012-04-20T01:51:40.800 回答