[Patchew-devel] [PATCH 3/4] models: final step in introducing Result.project field

Paolo Bonzini posted 4 patches 1 year, 8 months ago
[Patchew-devel] [PATCH 3/4] models: final step in introducing Result.project field
Posted by Paolo Bonzini 1 year, 8 months ago
Remove the pre-existing field in ProjectResult and rename the new field
from project_denorm to project.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 api/migrations/0069_auto_20220919_1008.py | 22 ++++++++++++++++++++++
 api/models.py                             | 20 +++++++++++++-------
 2 files changed, 35 insertions(+), 7 deletions(-)
 create mode 100644 api/migrations/0069_auto_20220919_1008.py

diff --git a/api/migrations/0069_auto_20220919_1008.py b/api/migrations/0069_auto_20220919_1008.py
new file mode 100644
index 0000000..dbd4ccc
--- /dev/null
+++ b/api/migrations/0069_auto_20220919_1008.py
@@ -0,0 +1,22 @@
+# Generated by Django 3.1.14 on 2022-09-19 10:08
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('api', '0068_auto_20220919_1007'),
+    ]
+
+    operations = [
+        migrations.RemoveField(
+            model_name='projectresult',
+            name='project',
+        ),
+        migrations.RenameField(
+            model_name='result',
+            old_name='project_denorm',
+            new_name='project',
+        ),
+    ]
diff --git a/api/models.py b/api/models.py
index 0728853..b942e20 100644
--- a/api/models.py
+++ b/api/models.py
@@ -63,7 +63,8 @@ class Result(models.Model):
     log_entry = models.OneToOneField(LogEntry, on_delete=models.CASCADE, null=True)
     data = jsonfield.JSONField(default={})
 
-    project_denorm = models.ForeignKey(
+    # This field is denormalized in the case of MessageResult
+    project = models.ForeignKey(
         "Project", related_name="+", on_delete=models.CASCADE
     )
 
@@ -331,14 +332,19 @@ class Project(models.Model):
         return len(updated_series)
 
     def create_result(self, **kwargs):
-        return ProjectResult(project=self, project_denorm=self, **kwargs)
+        return ProjectResult(project=self, **kwargs)
+
+    # Because Result's project field is used also for MessageResults (in which
+    # case it is a denormalized form of Message.project), it does not have
+    # a related_name; it would not make sense to return the MessageResults
+    # too.  Instead, this property returns a queryset of ProjectResults for
+    # this project.
+    @property
+    def results(self):
+        return ProjectResult.objects.filter(project=self)
 
 
 class ProjectResult(Result):
-    project = models.ForeignKey(
-        Project, related_name="results", on_delete=models.CASCADE, null=True
-    )
-
     @property
     def obj(self):
         return self.project
@@ -942,7 +948,7 @@ class Message(models.Model):
         emit_event("SeriesMerged", project=self.project, series=self)
 
     def create_result(self, **kwargs):
-        return MessageResult(message=self, project_denorm=self.project, **kwargs)
+        return MessageResult(message=self, project=self.project, **kwargs)
 
     def __str__(self):
         return self.project.name + "/" + self.subject
-- 
2.37.2

_______________________________________________
Patchew-devel mailing list
Patchew-devel@redhat.com
https://listman.redhat.com/mailman/listinfo/patchew-devel