-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(apis_metainfo): replace Uri.root_object with GenericForeignKey
The `Uri` should allow to point to any model instance, not only to models that inherit from `RootObject`. Therefore we introduce a GenericForeignKey. The migration copies the values of existing `root_object` pointers to the generic foreign key. The `root_object` field is then dropped from the model.
- Loading branch information
Showing
5 changed files
with
68 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
apis_core/apis_metainfo/migrations/0015_uri_content_type_uri_object_id.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Generated by Django 5.1.1 on 2024-11-29 08:31 | ||
|
||
import django.db.models.deletion | ||
from django.db import migrations, models | ||
|
||
|
||
def copy_root_object(apps, schema_editor): | ||
Uri = apps.get_model("apis_metainfo", "Uri") | ||
RootObject = apps.get_model("apis_metainfo", "RootObject") | ||
for uri in Uri.objects.all(): | ||
if uri.root_object_id: | ||
root_object = RootObject.objects.get(pk=uri.root_object_id) | ||
uri.content_type_id = root_object.self_contenttype_id | ||
uri.object_id = root_object.id | ||
uri.save() | ||
|
||
|
||
def reverse_copy_root_object(apps, schema_editor): | ||
Uri = apps.get_model("apis_metainfo", "Uri") | ||
for uri in Uri.objects.all(): | ||
if uri.content_type_id and uri.object_id: | ||
uri.root_object_id = uri.object_id | ||
uri.save() | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("apis_metainfo", "0014_remove_uri_domain_remove_uri_loaded_and_more"), | ||
("contenttypes", "0002_remove_content_type_name"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="uri", | ||
name="content_type", | ||
field=models.ForeignKey( | ||
null=True, | ||
on_delete=django.db.models.deletion.CASCADE, | ||
to="contenttypes.contenttype", | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="uri", | ||
name="object_id", | ||
field=models.PositiveIntegerField(null=True), | ||
), | ||
migrations.RunPython(copy_root_object, reverse_copy_root_object), | ||
migrations.RemoveField(model_name="uri", name="root_object"), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters