From a1c5819de09c5e84b6cfc0b201e3d45b0b32c705 Mon Sep 17 00:00:00 2001 From: Jean-Marie Favreau Date: Sat, 14 Sep 2024 23:40:41 +0200 Subject: [PATCH] =?UTF-8?q?On=20corrige=20la=20cat=C3=A9gorie=20par=20d?= =?UTF-8?q?=C3=A9faut?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0078_alter_event_category_and_more.py | 24 +++++++++++++++++++ src/agenda_culturel/models.py | 10 ++++---- 2 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 src/agenda_culturel/migrations/0078_alter_event_category_and_more.py diff --git a/src/agenda_culturel/migrations/0078_alter_event_category_and_more.py b/src/agenda_culturel/migrations/0078_alter_event_category_and_more.py new file mode 100644 index 0000000..8d2382c --- /dev/null +++ b/src/agenda_culturel/migrations/0078_alter_event_category_and_more.py @@ -0,0 +1,24 @@ +# Generated by Django 4.2.9 on 2024-09-14 13:18 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('agenda_culturel', '0077_category_position_alter_event_category_and_more'), + ] + + operations = [ + migrations.AlterField( + model_name='event', + name='category', + field=models.ForeignKey(default=1, help_text='Category of the event', null=True, on_delete=django.db.models.deletion.SET_DEFAULT, to='agenda_culturel.category', verbose_name='Category'), + ), + migrations.AlterField( + model_name='recurrentimport', + name='defaultCategory', + field=models.ForeignKey(default=1, help_text='Category of each imported event', on_delete=django.db.models.deletion.SET_DEFAULT, to='agenda_culturel.category', verbose_name='Category'), + ), + ] diff --git a/src/agenda_culturel/models.py b/src/agenda_culturel/models.py index 87753a8..ab9c209 100644 --- a/src/agenda_culturel/models.py +++ b/src/agenda_culturel/models.py @@ -137,6 +137,12 @@ class Category(models.Model): def get_default_category(): try: + # try to get an existing category + default = Category.objects.get(name=Category.default_name) + + return default + except: + # if it does not exist, return it default, created = Category.objects.get_or_create( name=Category.default_name, alt_name=Category.default_alt_name, @@ -144,10 +150,6 @@ class Category(models.Model): color=Category.default_color, ) - return default - except: - return None - def get_default_category_id(): cat = Category.get_default_category() if cat: