From 7da86924689386ffa7a29ec967054e3e5a51cc5e Mon Sep 17 00:00:00 2001 From: Birger Schacht Date: Mon, 11 Nov 2024 12:42:19 +0100 Subject: [PATCH] fix(models): allow generic foreign key to be empty --- ...lter_mongodbmodel_content_type_and_more.py | 25 +++++++++++++++++++ apis_instance_nsvis/models.py | 4 +-- 2 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 apis_instance_nsvis/migrations/0002_alter_mongodbmodel_content_type_and_more.py diff --git a/apis_instance_nsvis/migrations/0002_alter_mongodbmodel_content_type_and_more.py b/apis_instance_nsvis/migrations/0002_alter_mongodbmodel_content_type_and_more.py new file mode 100644 index 0000000..e6cdea5 --- /dev/null +++ b/apis_instance_nsvis/migrations/0002_alter_mongodbmodel_content_type_and_more.py @@ -0,0 +1,25 @@ +# Generated by Django 5.1.2 on 2024-11-11 09:26 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('apis_instance_nsvis', '0001_initial'), + ('contenttypes', '0002_remove_content_type_name'), + ] + + operations = [ + migrations.AlterField( + model_name='mongodbmodel', + name='content_type', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='contenttypes.contenttype'), + ), + migrations.AlterField( + model_name='mongodbmodel', + name='object_id', + field=models.PositiveIntegerField(null=True), + ), + ] diff --git a/apis_instance_nsvis/models.py b/apis_instance_nsvis/models.py index 57edb12..7584732 100644 --- a/apis_instance_nsvis/models.py +++ b/apis_instance_nsvis/models.py @@ -17,8 +17,8 @@ class NsvisMixin: class MongoDbModel(models.Model): data = models.JSONField() filename = models.TextField(blank=False, max_length=32) - content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE) - object_id = models.PositiveIntegerField() + content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE, null=True) + object_id = models.PositiveIntegerField(null=True) content_object = GenericForeignKey("content_type", "object_id")