diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f69dda6..12614839 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Added ### Changed +- Improve `Baker.get_fields()` to substract lists instead of extra set cast [PR #352](https://github.com/model-bakers/model_bakery/pull/352) ### Removed diff --git a/model_bakery/baker.py b/model_bakery/baker.py index 0f2ec28c..930f50b2 100644 --- a/model_bakery/baker.py +++ b/model_bakery/baker.py @@ -30,11 +30,7 @@ from django.db.models.fields.related import ( ReverseManyToOneDescriptor as ForeignRelatedObjectsDescriptor, ) -from django.db.models.fields.reverse_related import ( - ManyToManyRel, - ManyToOneRel, - OneToOneRel, -) +from django.db.models.fields.reverse_related import ManyToOneRel, OneToOneRel from . import generators, random_gen from ._types import M, NewM @@ -403,13 +399,12 @@ def prepare( params.update(attrs) return self._make(**params) - def get_fields(self) -> Any: - return set(self.model._meta.get_fields()) - set(self.get_related()) - - def get_related( - self, - ) -> List[Union[ManyToOneRel, OneToOneRel, ManyToManyRel]]: - return [r for r in self.model._meta.related_objects] + def get_fields(self) -> List[Any]: + return [ + f + for f in self.model._meta.get_fields() + if f not in self.model._meta.related_objects + ] def _make( self, @@ -474,7 +469,7 @@ def _make( _save_kwargs=_save_kwargs, ) if commit: - for related in self.get_related(): + for related in self.model._meta.related_objects: self.create_by_related_name(instance, related) if _refresh_after_create: