mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-04 04:20:12 +03:00
Add a new field to access external model fields
This commit is contained in:
parent
f5e98efcf2
commit
b21d20dc0e
|
@ -1844,3 +1844,17 @@ class ModelField(Field):
|
|||
if is_protected_type(value):
|
||||
return value
|
||||
return self.model_field.value_to_string(obj)
|
||||
|
||||
|
||||
class ExternalModelField(ModelField):
|
||||
"""
|
||||
Create a modelfield of an extenral class by accepting the class name as an
|
||||
argument.
|
||||
|
||||
This can help a lot while creating complex nested serializers
|
||||
"""
|
||||
def __init__(self, model_name, model_field, **kwargs):
|
||||
super(ExternalModelField, self).__init__(
|
||||
model_field=model_name()._meta.get_field(model_field),
|
||||
**kwargs
|
||||
)
|
Loading…
Reference in New Issue
Block a user