Fixed IntegrityError on postgres

pull/57/head
Fabio Caccamo 2019-02-06 16:21:50 +01:00
parent 4b055f7ffa
commit 4e0c4a00e2
1 changed files with 7 additions and 6 deletions

View File

@ -28,16 +28,17 @@ class Theme(models.Model):
@staticmethod @staticmethod
def get_active_theme(): def get_active_theme():
objs_active_qs = Theme.objects.filter(active=True) objs_manager = Theme.objects
objs_active_qs = objs_manager.filter(active=True)
objs_active_ls = list(objs_active_qs) objs_active_ls = list(objs_active_qs)
objs_active_count = len(objs_active_ls) objs_active_count = len(objs_active_ls)
if objs_active_count == 0: if objs_active_count == 0:
default_obj, default_obj_created = Theme.objects.get_or_create( obj = objs_manager.all().first()
pk='1', defaults={'active': True}) if obj:
if not default_obj_created: obj.set_active()
default_obj.set_active() else:
obj = default_obj obj = objs_manager.create()
elif objs_active_count == 1: elif objs_active_count == 1:
obj = objs_active_ls[0] obj = objs_active_ls[0]