Skip to content

Commit

Permalink
existing_user_fields: Fetch User model only when called
Browse files Browse the repository at this point in the history
Otherwise it may cause Django start up pains, and does cause migrations
to fail. Thanks to Jonathan Moss for reporting.
  • Loading branch information
maiksprenger committed Apr 2, 2014
1 parent 20df512 commit fa3f793
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
14 changes: 10 additions & 4 deletions oscar/core/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,24 @@ def get_user_model():
raise ImproperlyConfigured("AUTH_USER_MODEL must be of the form"
" 'app_label.model_name'")

_user_fields = get_user_model()._meta.fields
VALID_USER_FORM_FIELD_NAMES = set([field.name for field in _user_fields])


def existing_user_fields(fields):
"""
Starting with Django 1.6, the User model can be overridden and it is no
longer safe to assume the User model has certain fields. This helper
function assists in writing portable forms Meta.fields definitions
when those contain fields on the User model
Usage:
class UserForm(forms.Form):
...
class Meta:
# won't break if first_name is not defined on User model
fields = existing_user_fields(['first_name', 'last_name'])
"""
return list(set(fields) & VALID_USER_FORM_FIELD_NAMES)
user_fields = get_user_model()._meta.fields
user_field_names = [field.name for field in user_fields]
return list(set(fields) & set(user_field_names))

#
# Python3 compatibility layer
Expand Down
10 changes: 5 additions & 5 deletions tests/integration/customer/test_custom_user_model.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
from django.test import TestCase
from oscar.apps.customer.forms import ProfileForm
from oscar.core import compat
from oscar.core.compat import get_user_model, existing_user_fields


class TestCustomUserModel(TestCase):
class TestACustomUserModel(TestCase):

def setUp(self):
self.user_klass = compat.get_user_model()
self.user_klass = get_user_model()

def test_can_be_created_without_error(self):
try:
self.user_klass.objects.create_user('_', 'a@a.com', 'pa55w0rd')
except Exception, e:
self.fail("Unable to create user model: %s" % e)

def test_extra_field_is_picked_up(self):
self.assertTrue('extra_field' in compat.VALID_USER_FORM_FIELD_NAMES)
def test_extra_field_is_accessible(self):
self.assertTrue('extra_field' in existing_user_fields(['extra_field']))
self.assertTrue(hasattr(self.user_klass(), 'extra_field'))

def test_profile_form_doesnt_expose_extra_field(self):
Expand Down

0 comments on commit fa3f793

Please sign in to comment.