CMSTrackerDPG / MLplayground

Web application for collaborating to build ML models for Tracker DQM/DC
4 stars 2 forks source link

Provide Foreign Key value instead of id when using API #61

Closed XavierAtCERN closed 2 years ago

XavierAtCERN commented 2 years ago

Move from foreign key id to value for APIs in order to make results more relevant. To do so, see here.

nothingface0 commented 2 years ago

Proceeded with nested serializers.

Example for Tasks:

class TaskSerializer(serializers.ModelSerializer):
    training_runs = RunSerializer(many=True)
    testing_runs = RunSerializer(many=True)
    training_lumisections = LumisectionSerializer(many=True)
    testing_lumisections = LumisectionSerializer(many=True)

Returns JSON responses like the following:

{
            "id": 5,
            "training_runs": [
                {
                    "run_number": 297056,
                    "date": "2022-04-14T16:43:05.068086Z"
                },
                {
                    "run_number": 297225,
                    "date": "2022-04-14T17:00:58.981281Z"
                },
                {
                    "run_number": 297435,
                    "date": "2022-04-14T16:41:44.732728Z"
                }
            ],
            "testing_runs": [
                {
                    "run_number": 297056,
                    "date": "2022-04-14T16:43:05.068086Z"
                }
            ],
            "training_lumisections": [],
            "testing_lumisections": [],
            "name": "Task 1",
            "metadata": "Task 1",
            "created": "2022-05-13T16:03:50.348857Z",
            "modified": "2022-05-13T16:10:28.242086Z"
        }

Example for Predictions:

class PredictionSerializer(serializers.ModelSerializer):
    strategy = serializers.CharField(source="strategy.model")
    run_histograms = RunHistogramSerializer(many=True)
    lumisection_histograms_1d = LumisectionHistogram1DSerializer(many=True)
    lumisection_histograms_2d = LumisectionHistogram2DSerializer(many=True)

In this example, the task field is currently not serialized with TaskSerializer, which would include all the data included in the TaskSerializer response (see example JSON above), but only keeps the id of the task.