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: