1

I am trying to make a post request through insomnia to create a snake instance, but I am getting a Bad Request:400 error. I printed the serializer errors and got this:

{
    'owner': [ErrorDetail(string='Incorrect type. Expected URL string, received ReturnDict.', code='incorrect_type')], 
    'birthday': [ErrorDetail(string='This field is required.', code='required')], 
    'date_aquired': [ErrorDetail(string='This field is required.', code='required')], 
    'species': [ErrorDetail(string='Incorrect type. Expected URL string, received ReturnDict.', code='incorrect_type')], 
    'breeder_id': [ErrorDetail(string='This field is required.', code='required')], 
    'mother': [ErrorDetail(string='This field is required.', code='required')], 
    'father': [ErrorDetail(string='This field is required.', code='required')]
}

Here is my serializer which I thought would take care of the null/blank fields:

class SnakeDetailSerializer(serializers.HyperlinkedModelSerializer):
    href = serializers.HyperlinkedIdentityField(view_name="api_species_detail")
    birthday = serializers.DateField(allow_null=True)
    date_aquired = serializers.DateField(allow_null=True)
    enclosure_id = serializers.CharField(allow_null=True, allow_blank=True)
    breeder_id = serializers.CharField(allow_null=True, allow_blank=True)
    father = serializers.CharField(allow_null=True, allow_blank=True)
    mother = serializers.CharField(allow_null=True, allow_blank=True)


    class Meta:
        model = Snake
        fields = [
            'href',
            'id',
            'owner',
            'name',
            'age',
            'birthday',
            'date_aquired',
            'gender',
            'status',
            'weight',
            'enclosure_id',
            'species',
            'is_cbb',
            'is_imported',
            'breeder_id',
            'mother',
            'father'
            ]

Here is my model:

class Snake(models.Model):
    # BASIC INFO
    owner = models.ForeignKey(settings.AUTH_USER_MODEL,
        related_name="collection",
        on_delete=models.CASCADE
    )
    STATUS_CHOICES = (
        ('Active', 'Active'),
        ('Deceased', 'Deceased'),
        ('Quarantine', 'Quarantine'),
        ('For Sale', 'For Sale'),
        ('On Loan', 'On Loan'),
        ('Reserved', 'Reserved'),
        ('Sold', 'Sold')
    )
    GENDER_CHOICES = (
        ('M', 'Male'),
        ('F', 'Female'),
        ('NA', 'Unknown')
    )
    name = models.CharField(max_length=255)
    age = models.SmallIntegerField()
    birthday = models.DateField(null=True, blank=True)
    date_aquired = models.DateField(null=True, blank=True)
    status = models.CharField(max_length=10, choices=STATUS_CHOICES)
    gender = models.CharField(max_length=2, choices=GENDER_CHOICES)
    weight = models.DecimalField(max_digits=10, decimal_places=2)
    enclosure_id = models.CharField(max_length=255, null=True, blank=True)

    # SPECIES INFO
    species = models.ForeignKey(SpeciesInfo, related_name="snakes", on_delete=models.PROTECT)

    # LINEAGE INFO
    is_cbb = models.BooleanField(default=False)
    is_imported = models.BooleanField(default=False)
    breeder_id = models.CharField(max_length=255, null=True, blank=True)
    mother = models.CharField(max_length=255, null=True, blank=True)
    father = models.CharField(max_length=255, null=True, blank=True)

here is my view:

@api_view(['GET', 'POST'])
def api_list_snakes(request):
    if request.method == 'GET':
        snakes = Snake.objects.all()
        serializer = SnakeListSerializer(snakes, many=True)
        return Response(
            {'snakes': serializer.data}
         )
    else:
        data = JSONParser().parse(request)
        # print(data)
        species = SpeciesInfo.objects.get(id=data["species"])
        species_serialized = SpeciesInfoSerializer(species, context={'request':request})
        owner = GetUserSerializer(request.user)
        data['owner'] = owner.data
        data["species"] = species_serialized.data
        serializer = SnakeDetailSerializer(data=data, context={'request':request})

        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=201)
        print(serializer.errors)
        return Response(serializer.data, status=400)

I have also added a photo of the insomnia request.insomnia_request

I thought adding the allow_null and allow_blank on the fields would correct the bad request error but it doesn't seem to have changed anything I am also getting errors for my foreign key fields and I am not sure why?

Help would be very much appreciated! Thanks!

I tried to add the fields that were allowed to be blank/null to the serializer here:

class SnakeDetailSerializer(serializers.HyperlinkedModelSerializer):
    href = serializers.HyperlinkedIdentityField(view_name="api_species_detail")
    birthday = serializers.DateField(allow_null=True)
    date_aquired = serializers.DateField(allow_null=True)
    enclosure_id = serializers.CharField(allow_null=True, allow_blank=True)
    breeder_id = serializers.CharField(allow_null=True, allow_blank=True)
    father = serializers.CharField(allow_null=True, allow_blank=True)
    mother = serializers.CharField(allow_null=True, allow_blank=True)

and I expected that to correct the errors saying that they were required fields but they did not.

I also thought I had corrected the Foreign Key issues but it says they are expected a url string? I was using the id and it is getting the correct one so I'm not sure why it is mad about it

Lvermaelen
  • 11
  • 1

1 Answers1

0

I figured out how to fix my required field error. I had to add required=True to the serializer fields but I still can't get the foreign keys to work

Lvermaelen
  • 11
  • 1