made use of defaults in get_or_create

This commit is contained in:
Oskar Hahn 2012-11-22 18:47:50 +01:00
parent 1049141483
commit fa1c384d28
2 changed files with 4 additions and 8 deletions

View File

@ -28,10 +28,8 @@ class UserCreateForm(forms.ModelForm, CssClassMixin):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
if kwargs.get('instance', None) is None: if kwargs.get('instance', None) is None:
initial = kwargs.setdefault('initial', {}) initial = kwargs.setdefault('initial', {})
registered, created = Group.objects.get_or_create(name__iexact='Registered') registered, created = Group.objects.get_or_create(
if created: name__iexact='Registered', defaults={'name': 'Registered'})
registered.name = 'Registered'
registered.save()
initial['groups'] = [registered.pk] initial['groups'] = [registered.pk]
super(UserCreateForm, self).__init__(*args, **kwargs) super(UserCreateForm, self).__init__(*args, **kwargs)

View File

@ -253,9 +253,7 @@ def djangogroup_post_save(sender, instance, signal, *args, **kwargs):
def user_post_save(sender, instance, *args, **kwargs): def user_post_save(sender, instance, *args, **kwargs):
if not kwargs['created']: if not kwargs['created']:
return return
registered, created = Group.objects.get_or_create(name__iexact='Registered') registered, created = Group.objects.get_or_create(
if created: name__iexact='Registered', defaults={'name': 'Registered'})
registered.name = 'Registered'
registered.save()
instance.groups.add(registered) instance.groups.add(registered)
instance.save() instance.save()