From 1b2b3a3bceb315a746bc2758c728b66d6c6e724d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Am=C3=A9lie=20Krej=C4=8D=C3=AD?= Date: Sat, 23 Sep 2023 09:52:48 +0200 Subject: [PATCH] Get UserAttributes object using get_or_create This is done to automatically create the UserAttributes object in case LDAP User Backend didn't create it. --- admin/views.py | 2 +- instances/utils.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/admin/views.py b/admin/views.py index fd4ead4..8b71baf 100644 --- a/admin/views.py +++ b/admin/views.py @@ -117,7 +117,7 @@ def user_create(request): @superuser_only def user_update(request, pk): user = get_object_or_404(User, pk=pk) - attributes = UserAttributes.objects.get(user=user) + attributes, attributes_created = UserAttributes.objects.get_or_create(user=user) user_form = forms.UserForm(request.POST or None, instance=user) attributes_form = forms.UserAttributesForm( request.POST or None, instance=attributes diff --git a/instances/utils.py b/instances/utils.py index 53752b6..f9f5dcf 100644 --- a/instances/utils.py +++ b/instances/utils.py @@ -2,7 +2,7 @@ import os import random import string -from accounts.models import UserInstance +from accounts.models import UserInstance, UserAttributes from appsettings.settings import app_settings from django.conf import settings from django.utils.translation import gettext_lazy as _ @@ -26,7 +26,7 @@ def get_clone_free_names(size=10): def check_user_quota(user, instance, cpu, memory, disk_size): - ua = user.userattributes + ua, attributes_created = UserAttributes.objects.get_or_create(user=user) msg = "" if user.is_superuser: