Как удалить или пропустить сериализацию объектов в Django Rest Framework на основе условий?
class CreateAttributeSerializer(BaseAttributeSerializer):
class Meta(BaseAttributeSerializer.Meta):
fields=['id', 'required'] + BaseAttributeSerializer.Meta.fields
def to_representation(self, instance):
attribute = super().to_representation(instance)
current_display_order = int(instance.create_display_order)
old_instance = self.context.get('old_instance', None)
if old_instance:
attributes = []
old_attribute = self.context['old_attribute']
if int(old_instance.create_display_order) == current_display_order:
attributes = [old_attribute]
attributes.append(attribute)
else:
attributes.append(attribute)
return attributes
self.context['old_instance'] = instance
self.context['old_attribute'] = attribute
Мне нужно условно пропустить объект при сериализации в Django Rest Framework. При выполнении определенных условий объект не должен попадать в ответ вообще, ни в виде None, ни в виде пустого списка. Как я могу избавиться от null,
[
null,
[
{
"id": 1,
"required": false,
"name": "price",
"slug": "price"
},
{
"id": 6,
"required": true,
"name": "currency",
"slug": "currency",
"choices": [
"sum",
"y.e"
]
}
],
[
{
"id": 2,
"required": false,
"name": "Chevrolet model",
"slug": "chevrolet_model",
"choices": [
"Nexia",
"Damas"
]
}
],
]