Django Rest Framework when doing a POST getting TypeError at Function.objects.create()

I have the following model

class Funcion(models.Model):
    idPelicula = models.ForeignKey(Pelicula, on_delete=models.PROTECT)
    idSala = models.ForeignKey(Sala, on_delete=models.PROTECT)
    horaEntrada = models.TimeField(auto_now=False, auto_now_add=False)
    horaSalida = models.TimeField(auto_now=False, auto_now_add=False)
    fecha = models.DateField(auto_now=False)

And the next serializer

class FuncionSerializer(serializers.ModelSerializer):
    pelicula = PeliculaSerializer(read_only=True)
    idPelicula = serializers.PrimaryKeyRelatedField(write_only=True, queryset=Pelicula.objects.all(), source='pelicula')
    sala = SalaSerializer(read_only=True)
    idSala = serializers.PrimaryKeyRelatedField(write_only=True, queryset=Sala.objects.all(), source='sala')

    class Meta:
        model = Funcion
        fields = '__all__'

When I try to post a new Funcion with the Django api root I get the following error:

TypeError at /funcion/

Got a `TypeError` when calling `Funcion.objects.create()`. This may be because you have a writable field on the serializer class that is not a valid argument to `Funcion.objects.create()`. You may need to make the field read-only, or override the FuncionSerializer.create() method to handle this correctly.

I have used this method before for other project practices and it worked correctly.

If i delete the source='pelicula' argument for the PrimaryKeyRelatedField() it post the new funcion but when I do a get to bring al the data it doesn't show the Pelicula or Sala field just the rest

I tried deleting those arguments because the error at the end shows this message TypeError: Funcion() got unexpected keyword arguments: 'pelicula', 'sala'

The create method of the model manager does not allow "unexpected" fields. Those fields are not properties or fields of that model.

Unfortunately as far as I understand the code, the field slips through the field building functions and other into the validated_data dictionary. This is probably because every read_only field is allowed as it would be filtered out anyway before reaching the validated_data, but sourceing a read_only field goes through. I do not believe this is expected, but still causes no harm since the model managers' create method raises a TypeError.

To circumvent that issue you have two main options:

  1. The source argument of your field may only reference fields or properties that exist on your model.

  2. Pop any fields that are not part of the model out before being passed to the create method of the model.

The last option would look something like this:

class CoolSerializer(serializers.ModelSerializer):
    not_cool = serializers.CharField(max_length=42)

    def create(self, validated_data):
        cool_again = validated_data.pop('not_cool', None)
        # Do something with cool_again that you originally intended to do.
        return super().create(validated_data)
Back to Top