简体   繁体   English

在Django中根据请求自动添加新的model_field || DRF

[英]Auto add new model_field on request in Django || DRF

I have a simple REST-API project. 我有一个简单的REST-API项目。

Here is my models: 这是我的模型:

class Human(Model):
    height = IntegerField()

And I have a serializer for that: 我有一个序列化器:

class HumanSerializer(ModelSerializer):
    id = IntegerField(read_only=True)
    height = IntegerField(required=True)

Very clear and simple! 非常清楚和简单! But! 但! What I want is to be able to add additional fields to my Human model, and automatically migrate those changes to the database, and add a new field to serializer that would be related to the new model field. 我想要的是能够向我的人员模型添加其他字段,并自动将这些更改迁移到数据库,并向与新模型字段相关的序列化器添加一个新字段。

For example, lets say that the client want to add a new field called "weight". 例如,假设客户想要添加一个名为“ weight”的新字段。 So a developer won't do anything, because the backend will automatically receive that request, will automatically create a new field to Human model, automatically create a new field to serializer, and automatically create a migration.py file related to those changes. 因此开发人员不会做任何事情,因为后端将自动接收该请求,将自动为Human模型创建一个新字段,为序列化器自动创建一个新字段,并自动创建与这些更改相关的migration.py文件。

Is there a way to achieve that ? 有办法实现吗? What is the best way to do that ? 最好的方法是什么?

In case the Human model has no relations (like a ForeignKey , ManyToManyField , OneToOneField , etc.) to models, then you can simply specify this with fields = '__all__' : 如果Human模型与模型没有任何关系(例如ForeignKeyManyToManyFieldOneToOneField等),则可以使用fields = '__all__'来简单地指定它:

class HumanSerializer(ModelSerializer):
    class Meta:
        model = Human
        fields = '__all__' 

But relations to other models will still have to be included manually, since there can be several ways to handle this: 但是与其他模型的关系仍然必须手动包括,因为可以通过几种方法来解决此问题:

  1. by ignoring them; 通过忽略它们;
  2. by adding the id (or URI) to the object; 通过将id (或URI)添加到对象中;
  3. by serializing the related objects as well; 通过序列化相关对象;
  4. ... ...

Perhaps serializing the related objects looks like a straightforward choice, but this can result in infinite recursion if for example an object (perhaps indirectly) points to itself, so that would mean that the serialization of that objects contains the serialization of that object. 也许序列化相关对象看起来像是一个简单的选择,但是如果例如某个对象(可能是间接指向)指向自己,这可能会导致无限递归,因此这意味着该对象的序列化包含该对象的序列化。 Even if it does not create recursive loops, it can still blow up the response gigantically, so even if this recursion is not possible, it might not be ideal to transfer thousands of objects in a single response. 即使它不创建递归循环,它仍然可以极大地放大响应,因此即使无法进行递归,在单个响应中传输数千个对象可能也不理想。 Finally a reason why this might not be advisable is that through those relations, we might end up into models that store sensitive information (for example if an object is linked to a model that is linked to one or more users, then your API would expose details of the users), which might not be a good idea. 最后,可能不建议这样做的原因是,通过这些关系,我们可能最终进入存储敏感信息的模型(例如,如果对象链接到链接到一个或多个用户的模型,那么您的API将会暴露用户的详细信息),这可能不是一个好主意。

If for example Human would have a reference to a (hypothetical) Ssn object, then you can serialize this as well with: 例如,如果“ Human引用了一个(假设的) Ssn对象,那么您也可以使用以下方法序列化该对象:

class HumanSerializer(ModelSerializer):
    ssn = SsnSerializer(read_only=True)

    class Meta:
        model = Human
        fields = '__all__'

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

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