From 6281bcae26a94328557cece209448e21d198f1d4 Mon Sep 17 00:00:00 2001 From: Hironsan Date: Tue, 22 Feb 2022 16:11:05 +0900 Subject: [PATCH] Rename RelationNew to Relation --- .../0010_rename_relationnew_relation.py | 21 +++++++++++++++++++ backend/labels/models.py | 2 +- backend/labels/serializers.py | 11 +++++----- backend/labels/views.py | 6 +++--- 4 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 backend/labels/migrations/0010_rename_relationnew_relation.py diff --git a/backend/labels/migrations/0010_rename_relationnew_relation.py b/backend/labels/migrations/0010_rename_relationnew_relation.py new file mode 100644 index 00000000..2d112453 --- /dev/null +++ b/backend/labels/migrations/0010_rename_relationnew_relation.py @@ -0,0 +1,21 @@ +# Generated by Django 4.0.2 on 2022-02-22 07:04 + +from django.conf import settings +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ("label_types", "0007_delete_relationtypeold"), + ("examples", "0002_alter_example_project"), + ("labels", "0009_delete_relationold"), + ] + + operations = [ + migrations.RenameModel( + old_name="RelationNew", + new_name="Relation", + ), + ] diff --git a/backend/labels/models.py b/backend/labels/models.py index ac8d947d..6a788618 100644 --- a/backend/labels/models.py +++ b/backend/labels/models.py @@ -90,7 +90,7 @@ class TextLabel(Label): unique_together = ("example", "user", "text") -class RelationNew(Label): +class Relation(Label): from_id = models.ForeignKey(Span, on_delete=models.CASCADE, related_name="from_relations") to_id = models.ForeignKey(Span, on_delete=models.CASCADE, related_name="to_relations") type = models.ForeignKey(RelationType, on_delete=models.CASCADE) diff --git a/backend/labels/serializers.py b/backend/labels/serializers.py index f7209c01..dc7132fa 100644 --- a/backend/labels/serializers.py +++ b/backend/labels/serializers.py @@ -1,8 +1,8 @@ from rest_framework import serializers -from .models import Category, RelationNew, Span, TextLabel +from .models import Category, Relation, Span, TextLabel from examples.models import Example -from label_types.models import CategoryType, SpanType +from label_types.models import CategoryType, RelationType, SpanType class CategorySerializer(serializers.ModelSerializer): @@ -61,9 +61,10 @@ class TextLabelSerializer(serializers.ModelSerializer): class RelationSerializer(serializers.ModelSerializer): - def validate(self, attrs): - return super().validate(attrs) + example = serializers.PrimaryKeyRelatedField(queryset=Example.objects.all()) + type = serializers.PrimaryKeyRelatedField(queryset=RelationType.objects.all()) class Meta: - model = RelationNew + model = Relation fields = ("id", "prob", "user", "example", "created_at", "updated_at", "from_id", "to_id", "type") + read_only_fields = ("user",) diff --git a/backend/labels/views.py b/backend/labels/views.py index e485f22b..f2c4cfc3 100644 --- a/backend/labels/views.py +++ b/backend/labels/views.py @@ -14,7 +14,7 @@ from .serializers import ( SpanSerializer, TextLabelSerializer, ) -from labels.models import Category, Label, RelationNew, Span, TextLabel +from labels.models import Category, Label, Relation, Span, TextLabel from projects.models import Project from projects.permissions import IsProjectMember @@ -118,12 +118,12 @@ class RelationList(generics.ListCreateAPIView): def delete(self, request, *args, **kwargs): delete_ids = request.data["ids"] - RelationNew.objects.filter(pk__in=delete_ids).delete() + Relation.objects.filter(pk__in=delete_ids).delete() return Response(status=status.HTTP_204_NO_CONTENT) class RelationDetail(generics.RetrieveUpdateDestroyAPIView): - queryset = RelationNew.objects.all() + queryset = Relation.objects.all() serializer_class = RelationSerializer lookup_url_kwarg = "annotation_id" permission_classes = [IsAuthenticated & IsProjectMember]