Exemple #1
0
 def set_value(self, value):
     if value is None:
         return
     elif isinstance(value, Iterable):
         values = value
     elif isinstance(value, (QuerySet, Manager)):
         if self.only_fields or self.exclude:
             local_fields = get_local_fields(value.model)
             related_fields = get_related_fields(value.model)
             only_fields = [f.name for f in local_fields]
             if self.only_fields:
                 only_fields = [f for f in only_fields if f in self.only_fields]
             exclude_fields = [f.name for f in local_fields if f.name in self.exclude]
             if exclude_fields:
                 only_fields = [f for f in only_fields if f not in exclude_fields]
             only_fields += [f.name for f in related_fields]
             # .only() returns a QuerySet of RelatedDjangoModel_Deferred objects?
             values = value.only(*only_fields)
         else:
             values = value.all()
     else:
         return
     self.items[:] = []
     self._native_items[:] = []
     self._python_items[:] = []
     for item in values:
         self.add_item(source=item)
Exemple #2
0
 def set_fields_from_model(cls, new_class, fields, meta):
     if django_models is None or meta.model is None:
         return
     meta.parents.handle(meta.model)
     all_field_names = cls.get_all_fieldnames(fields)
     for model_field in django_utils.get_local_fields(meta.model):
         if model_field.name not in all_field_names:
             serializer_field = cls.add_local_model_field(fields, model_field, meta=meta)
             if serializer_field:
                 setattr(new_class, model_field.name, serializer_field)
     for model_field in django_utils.get_related_fields(meta.model):
         if model_field.name not in all_field_names:
             serializer_field = cls.add_relation_model_field(fields, model_field, meta=meta)
             if serializer_field:
                 setattr(new_class, model_field.name, serializer_field)
     for model_field in django_utils.get_reverse_related_fields(meta.model):
         field_name = django_utils.get_reverse_related_name_from_field(model_field)
         if field_name not in all_field_names:
             serializer_field = cls.add_reverse_relation_model_field(fields, model_field, meta=meta)
             if serializer_field:
                 setattr(new_class, field_name, serializer_field)