简体   繁体   English

在DRF序列化程序上为ManyToMany和OneToMany字段使用自定义模型管理器

[英]Use custom model manager for ManyToMany and OneToMany fields on DRF serializer

I have overridden the default model manager objects to only return objects for which is_deleted is no True. 我已经重写了默认的模型管理器objects ,只返回is_deleted为True的对象。

+------+------------+
| Name | is_deleted |  
+------+------------+
| foo  | True       |  
| bar  | False      |  
| baz  | False      |  
+------+------------+

So, whenever I do MyModel.objects.all() gives me - 所以,每当我做MyModel.objects.all()给我 -

+------+------------+
| Name | is_deleted |  
+------+------------+
| bar  | False      |  
| baz  | False      |  
+------+------------+

But ManyToMany field is not respecting my objects manager. ManyToMany字段不尊重我的objects管理器。 When I do user.foo_set.all() , I get all the objects, which I don't want. 当我执行user.foo_set.all() ,我得到了所有我不想要的对象。 Is there a way around to do it? 有没有办法做到这一点?

I know that I can do user.foo_set(manager='objects').all() and it'll pick my object manager and give only the non deleted items but here's the problem - 我知道我可以执行user.foo_set(manager='objects').all()并且它将选择我的对象管理器并仅提供未删除的项目,但这里是问题 -

I'm using Django Rest Framework and for serializing ToMany fields, it would give me all objects, regardless of their is_deleted value. 我正在使用Django Rest Framework并序列化ToMany字段,它会给我所有对象,无论它们的is_deleted值如何。 Is there a way to enforce the object manager on the serializer for ToMany fields? 有没有办法在ToMany字段的序列化程序上强制执行对象管理器?

Right now, I'm using this, which seems kind of hack-ish - 现在,我正在使用这个,这似乎是一种黑客攻击 -

class MySerializer(serializers.HyperlinkedModelSerializer):
    things = serializers.SerializerMethodField()

    class Meta:
        model = MyModel
        fields = ('id', 'things')

    def get_things(self, obj):
        return many_field_serialize(obj, 'things', ThingSerializer)





def many_field_serialize(obj, field_name, serializer_class):
    """
    Serializer, by default would give us all the related results, while we
    want to ignore the ones which have been marked as deleted or archived.
    We have our custom modele manager for that - `objects` but serializer
    doesn't use that. So the only solution at this time seems to manually
    get all the related fields and apply our `objects` model manager on it.

    There should be a better way to do it, or we'll keep duplicating this
    logic for every related field serializer.
    """
    items = getattr(obj, field_name)(manager='objects').all()
    serialized_items = serializer_class(
        instance=items, many=True
    )
    return serialized_items.data

Finally figured it out. 终于想通了。 All I had to do was make the objects model manager default (By defining it as the first manager in model), and in the model manager set use_for_related_fields = True , so that the related fields would use your base model manager by default. 我所要做的就是使objects模型管理器默认(通过将其定义为模型中的第一个管理器),并在模型管理器中设置use_for_related_fields = True ,以便相关字段默认使用基础模型管理器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM