Example #1
0
 def add_relation_model_field(cls, fields, model_field, meta, **kwargs):
     if django_utils.is_relation_field(model_field):
         relation_parents_manager = meta.parents.get_working_copy()
         rel_django_model = django_utils.get_related_model_from_field(model_field)
         if not relation_parents_manager.handle(rel_django_model):
             return None
         if model_field.primary_key:
             kwargs['identity'] = True
             kwargs['required'] = False
             kwargs['on_null'] = serializer_fields.HIDE_FIELD
         if model_field.null or model_field.blank:
             kwargs['required'] = False
         field_kwargs = meta.field_arguments.get_nested_field_kwargs(model_field.name)
         serializer_cls = cls.get_nested_serializer_class(rel_django_model,
                                                          relation_parents_manager,
                                                          **field_kwargs)
         if isinstance(model_field, django_models.ManyToManyField):
             field_class = RelatedManagerListSerializerField
         else:
             field_class = serializer_fields.SerializerField
         kwargs = meta.field_arguments.parse(model_field.name, **kwargs)
         _field = field_class(serializer_cls, **kwargs)
         _field.add_name(model_field.name)
         fields[model_field.name] = _field
         return _field
     return None
Example #2
0
 def add_reverse_relation_model_field(cls, fields, model_field, meta, **kwargs):
     if django_utils.is_reverse_relation_field(model_field):
         field_name = django_utils.get_reverse_related_name_from_field(model_field)
         relation_parents_manager = meta.parents.get_working_copy()
         rel_django_model = django_utils.get_related_model_from_field(model_field)
         if not relation_parents_manager.handle(rel_django_model):
             return None
         field_kwargs = meta.field_arguments.get_nested_field_kwargs(field_name)
         serializer_cls = cls.get_nested_serializer_class(rel_django_model,
                                                          relation_parents_manager,
                                                          **field_kwargs)
         if django_utils.is_reverse_one2one_relation_field(model_field):
             field_class = serializer_fields.SerializerField
         else:
             field_class = RelatedManagerListSerializerField
         kwargs = meta.field_arguments.parse(model_field.name, **kwargs)
         _field = field_class(serializer_cls, required=False, **kwargs)
         _field.add_name(django_utils.get_reverse_related_name_from_field(model_field))
         fields[field_name] = _field
         return _field
     return None