E
E
Enter_a_nickname2022-02-15 18:04:48
Django
Enter_a_nickname, 2022-02-15 18:04:48

Many-to-many or ForeignKey?

My django project needs to implement api with get method.
Here is part of my code:

models.py:

class Color(models.Model):
    temp = models.IntegerField(max_value=None, min_value=1)
    correct = models.BooleanField()
    top = models.IntegerField(max_value=None, min_value=1)
    bottom = models.IntegerField(max_value=None, min_value=1)

class Speed(models.Model):
    start = models.TimeField()
    end = models.TimeField()

class Dog(models.Model):
    name = models.CharField(max_length=50)
    number = models.IntegerField(max_value=None, min_value=1)
    speed = models.ForeignKey('Speed', on_delete=models.CASCADE, null=True, blank=True)

class DogRequest(models.Model):
    id_name = models.IntegerField(max_value=None, min_value=1)
    colors = models.ForeignKey('color', on_delete=models.CASCADE, null=True, blank=True)
    dogs = models.ForeignKey('dog', on_delete=models.CASCADE, null=True, blank=True)
    DataTime = models.DateTimeField()


serializers.py:

class ColorSerilizer(serializers.Serializer):
    temp = serializers.IntegerField(max_value=None, min_value=1)
    correct = serializers.BooleanField()
    top = serializers.IntegerField(max_value=None, min_value=1)
    bottom = serializers.IntegerField(max_value=None, min_value=1)

class SpeedSerializer(serializers.Serializer):
    start = serializers.TimeField()
    end = serializers.TimeField()

class DogSerializer(serializers.Serializer):
    name = serializers.CharField(max_length=50)
    number = serializers.IntegerField(max_value=None, min_value=1)
    speed = SpeedSerializer()

class DogRequestSerializer(serializers.Serializer):
    id_name = serializers.IntegerField(max_value=None, min_value=1)
    color = ColorSerilizer()
    dog = DogSerializer()
    DataTime = serializers.DateTimeField()


my views.py:

@csrf_exempt
def DogRequestApi(request, id=0):
    if 'GET' == request.method:
        DogRequests = DogRequest.objects.all()
         DogRequest_serializer = DogRequestSerializer(DogRequests)
        return JsonResponse(DogRequest_serializer.data, safe=False)


When doing a get request through postman, I get the following structure:

{
    "id_name": 44,
    "color": {
        "temp": 33,
        "correct": true,
        "top": 33,
        "bottom": 33,
    },
    "dog": {
        "name": "bobi",
        "number": 33,
        "speed": {
            "start": "10:07:21",
            "end": "10:07:22"
        }
    },
    "DataTime": "2022-02-11T10:07:29Z"


It is necessary to correct one nuance, there may be several "dog" blocks.
How to make the following structure?

{
    "id_name": 44,
    "color": {
        "temp": 33,
        "correct": true,
        "top": 33,
        "bottom": 33,
    },
    "dog": [{
        "name": "bobi",
        "number": 33,
        "speed": {
            "start": "10:07:21",
            "end": "10:07:22"
        }
    },
    "DataTime": "2022-02-11T10:07:29Z"
   }]...


And so, the crux of the matter:
1) I need a DogRequest to be able to have multiple Dogs, and a Dog can only refer to 1 DogRequest. As far as I understood from the Django documentation, the many-to-one relationship is exactly what ForeignKey is. So is this or is it still necessary to use many-to-many?
2) How to write model data in my case?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2022-02-15
@bacon

I need a DogRequest to be able to have multiple Dogs and a Dog can only refer to 1 DogReques
at Dog you do ForeignKey on DogRequest

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question