-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make
LabeledDataSerializer.validated_data
usable (#8144)
Currently, the views that use `LabeledDataSerializer` for input do something unconventional: they create the serializer with the input data, call `is_valid()`, but then use the original data instead of the `validated_data` member. I believe this is because `validated_data` in this case is unusable because of the `source` attributes on some of the fields in the nested serializers. For example, in `LabeledImageSerializer`, the `attributes` field has a source of `labeledimageattributeval_set`. This works well when serializing `LabeledImage` objects, but when you're deserializing, this creates a dict with a `labeledimageattributeval_set` key. Such objects are incompatible with functions like `patch_job_data`, which expect the `attributes` key instead. In the current code, using `data` instead of `validated_data` seems to work okay-ish. It _is_ a bit confusing, though, because it's unconventional. For example, the `default` values set in serializer fields are effectively useless, because they're only filled in `validated_data`. However, I'm currently working on a feature where a `LabeledDataSerializer` is incorporated into another serializer, and this problem means that I can't use `validated_data` for the parent serializer either, and that means I can't implement custom `to_internal_value` or `create` methods. So I'd much rather fix this. While we could do it by making `patch_job_data` and others accept `labeledimageattributeval_set`, this seems counterproductive. The name `attributes` is much easier to read & understand. So instead, change the models so that the attributes of an annotation object can be accessed via `.attributes` and the shapes of a track via `.tracks`. That way, the `source` attributes become unnecessary. This fixes the problem _and_ makes the code clearer at the same time.
- Loading branch information
Showing
6 changed files
with
147 additions
and
96 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
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
54 changes: 54 additions & 0 deletions
54
cvat/apps/engine/migrations/0079_alter_labeledimageattributeval_image_and_more.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,54 @@ | ||
# Generated by Django 4.2.13 on 2024-07-09 11:08 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("engine", "0078_alter_cloudstorage_credentials"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="labeledimageattributeval", | ||
name="image", | ||
field=models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="attributes", | ||
related_query_name="attribute", | ||
to="engine.labeledimage", | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name="labeledshapeattributeval", | ||
name="shape", | ||
field=models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="attributes", | ||
related_query_name="attribute", | ||
to="engine.labeledshape", | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name="labeledtrackattributeval", | ||
name="track", | ||
field=models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="attributes", | ||
related_query_name="attribute", | ||
to="engine.labeledtrack", | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name="trackedshapeattributeval", | ||
name="shape", | ||
field=models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="attributes", | ||
related_query_name="attribute", | ||
to="engine.trackedshape", | ||
), | ||
), | ||
] |
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
Oops, something went wrong.