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):
if kwargs.get('instance', None) is None:
initial = kwargs.setdefault('initial', {})
registered, created = Group.objects.get_or_create(name__iexact='Registered')
if created:
registered.name = 'Registered'
registered.save()
registered, created = Group.objects.get_or_create(
name__iexact='Registered', defaults={'name': 'Registered'})
initial['groups'] = [registered.pk]
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):
if not kwargs['created']:
return
registered, created = Group.objects.get_or_create(name__iexact='Registered')
if created:
registered.name = 'Registered'
registered.save()
registered, created = Group.objects.get_or_create(
name__iexact='Registered', defaults={'name': 'Registered'})
instance.groups.add(registered)
instance.save()