Home > Mobile >  Django REST: ignoring custom fields which are not part of model
Django REST: ignoring custom fields which are not part of model

Time:12-14

My TimeReport model looks like this:


class TimeReport(models.Model):
    minutes_spent = models.PositiveIntegerField()
    task = models.ForeignKey(Task, on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)
    modified_at = models.DateTimeField(auto_now=True)
    reported_for = models.DateField()
    note = models.TextField(null = True, blank=True)
    status = models.CharField(max_length=50, choices=State.choices, default=State.new)
    user = models.ForeignKey(User, on_delete=models.PROTECT)

And my model serializer:

class TimeReportCreateSerializer(serializers.ModelSerializer):
    class Meta:
        model = TimeReport
        fields = (
            'id',
            'minutes_spent',
            'reported_for', 
            'note',
            'status',
            'task_custom_id',
            )
    
    task_custom_id = serializers.CharField()

    def create(self, validated_data):
        user = User.objects.get(auth_user_id = self.context['user_id'])
        task = Task.objects.filter(custom_id = validated_data['task_custom_id']).filter(user = user.id).first()
        
        report = TimeReport(**validated_data)
        report.user = user
        report.task = task
        report.save()
        return report

So, the problem is, that I want to take a custom value in a serializer, which is not a part of a model and do some custom logic with it - in this case search for the right 'task' in the database. But when I try to parse the model by using report = TimeReport(**validated_data), it gives me an exception:

TypeError at /api/report/
TimeReport() got an unexpected keyword argument 'task_custom_id'

Im kind of new to Django and python itself, so - what is the best approach?

CodePudding user response:

If you are going to use that field only for creation, you should use write_only option.

task_custom_id = serializers.CharField(write_only=True)

See the docs here https://www.django-rest-framework.org/api-guide/fields/#write_only

CodePudding user response:

You just need to remove task_custom_id from the dictionary

class TimeReportCreateSerializer(serializers.ModelSerializer):
    class Meta:
        model = TimeReport
        fields = (
            'id',
            'minutes_spent',
            'reported_for', 
            'note',
            'status',
            'task_custom_id',
            )
    
    task_custom_id = serializers.CharField()

    def create(self, validated_data):
        user = User.objects.get(auth_user_id = self.context['user_id'])
        task_custom_id = validated_data.pop("task_custom_id")
        task = Task.objects.filter(custom_id = task_custom_id).filter(user = user.id).first()
        report = TimeReport(**validated_data)
        report.user = user
        report.task = task
        report.save()
        return report
  • Related