Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Invalid `self.instance` when validating the serializer using `many=True`

Aperta
#8,926 19 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@zainnadeem786 ci sta già lavorando.

Dal 25/1/2026.

  • #8979 di @saadullahaleem — integrata
  • #9774 di @p-r-a-v-i-n — aperta
  • #9879 di @zainnadeem786 — aperta

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
42/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Tranquilla
Stack tecnologico
django, python
Ambito
api, backend

Direzione di ricerca

La issue non indica file di implementazione né test; inizia tracciando la validazione del serializer attraverso il percorso many=True di ListSerializer e il modo in cui ogni child riceve la propria instance durante la validazione parziale. Il lavoro è completato quando la validazione bulk usa l’oggetto corrispondente per self.instance, mantiene il comportamento della singola instance e restituisce gli errori per elemento mostrati nell’esempio.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Bug Needs design decision

Currently it is almost impossible to use the same serializer with and without many=True and the problem that sometimes serializer depending on the instance state or value during serialization and validation (usually it is PATCH method and partial=True

This is the simplest example to understand:

from django.db import models
from rest_framework import serializers


class MyClass(models.Model):
    name = models.CharField(max_length=100)
    status = models.CharField(max_length=100, blank=True)

    @property
    def is_valid(self):
        return self.name == 'valid'


class MyClassSerializer(serializers.ModelSerializer):
    class Meta:
        model = MyClass
        fields = ('id', 'name', 'status')

    def validate_status(self, value):
        if value and not self.instance.is_valid:
            raise serializers.ValidationError('Status cannot be set for invalid instance')
        return value


objs = MyClass.objects.bulk_create(
    objs=[
        MyClass(name='valid'),
        MyClass(name='invalid'),
        MyClass(name='other'),
    ]
)

serializer = MyClassSerializer(
    data=[{'status': 'set', 'id': instance.id} for instance in objs],
    instance=objs,
    many=True,
    partial=True,
)
serializer.is_valid()
print(serializer.errors)

Please note that it is just an example and real serializers and models can be much more complex. The case is working when we pass the single instance to the serializer but not working when we are trying to validate bulk operation, like update in bulk (let's say)

The following exception raised:

AttributeError: 'list' object has no attribute 'is_valid'

When expected behavior would be to get this one (pretty-printed):

[
    {},
    {'status': [ErrorDetail(string='Status cannot be set for invalid instance', code='invalid')]},
    {'status': [ErrorDetail(string='Status cannot be set for invalid instance', code='invalid')]},
]

P.S. I was able to manually patch the serializer to obtain the instance from the data provided, but I do not think that it should be done manually for every serializer. I assume that when we are validating the instance using PARTIAL, then the appropriate instance which is currently on validation, should be available inside the serializer. In this case we will be able to use the same serializer as single and multi entity-validation.

P.P.S. Also I think that if user require some logic for the list of objects, it should be placed inside the overridden list_serializer_class, no?

Lingua principale
Python
Stelle
30.2k
Fork
7.1k
Merge medio
1g 20h
PR unite (30g)
19

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di encode/django-rest-framework

Tutte le issue di encode/django-rest-framework

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.