model-bakery 1.18.2__py3-none-any.whl → 1.19.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of model-bakery might be problematic. Click here for more details.

model_bakery/__about__.py CHANGED
@@ -1 +1 @@
1
- __version__ = "1.18.2"
1
+ __version__ = "1.19.0"
model_bakery/baker.py CHANGED
@@ -51,7 +51,7 @@ from .utils import (
51
51
 
52
52
  if BAKER_CONTENTTYPES:
53
53
  from django.contrib.contenttypes import models as contenttypes_models
54
- from django.contrib.contenttypes.fields import GenericRelation
54
+ from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
55
55
  else:
56
56
  contenttypes_models = None
57
57
  GenericRelation = None
@@ -501,6 +501,7 @@ class Baker(Generic[M]):
501
501
  ) -> M:
502
502
  one_to_many_keys = {}
503
503
  auto_now_keys = {}
504
+ generic_foreign_keys = {}
504
505
 
505
506
  for k in tuple(attrs.keys()):
506
507
  field = getattr(self.model, k, None)
@@ -517,12 +518,15 @@ class Baker(Generic[M]):
517
518
  ):
518
519
  auto_now_keys[k] = attrs[k]
519
520
 
521
+ if BAKER_CONTENTTYPES and isinstance(field, GenericForeignKey):
522
+ generic_foreign_keys[k] = attrs.pop(k)
523
+
520
524
  instance = self.model(**attrs)
521
- # m2m only works for persisted instances
522
525
  if _commit:
523
526
  instance.save(**_save_kwargs)
524
527
  self._handle_one_to_many(instance, one_to_many_keys)
525
528
  self._handle_m2m(instance)
529
+ self._handle_generic_foreign_keys(instance, generic_foreign_keys)
526
530
  self._handle_auto_now(instance, auto_now_keys)
527
531
 
528
532
  if _from_manager:
@@ -622,7 +626,7 @@ class Baker(Generic[M]):
622
626
 
623
627
  if field.name not in self.model_attrs: # noqa: SIM102
624
628
  if field.name not in self.rel_fields and (
625
- field.null and not field.fill_optional
629
+ not field.fill_optional and field.null
626
630
  ):
627
631
  return True
628
632
 
@@ -682,6 +686,10 @@ class Baker(Generic[M]):
682
686
  }
683
687
  make(through_model, _using=self._using, **base_kwargs)
684
688
 
689
+ def _handle_generic_foreign_keys(self, instance: Model, attrs: Dict[str, Any]):
690
+ for key, value in attrs.items():
691
+ setattr(instance, key, value)
692
+
685
693
  def _remote_field(
686
694
  self, field: Union[ForeignKey, OneToOneField]
687
695
  ) -> Union[OneToOneRel, ManyToOneRel]:
@@ -702,12 +710,16 @@ class Baker(Generic[M]):
702
710
  model.
703
711
  """
704
712
  is_content_type_fk = False
713
+ is_generic_fk = False
705
714
  if BAKER_CONTENTTYPES:
706
715
  is_content_type_fk = isinstance(field, ForeignKey) and issubclass(
707
716
  self._remote_field(field).model, contenttypes_models.ContentType
708
717
  )
718
+ is_generic_fk = isinstance(field, GenericForeignKey)
719
+ if is_generic_fk:
720
+ generator = self.type_mapping[GenericForeignKey]
709
721
  # we only use default unless the field is overwritten in `self.rel_fields`
710
- if field.has_default() and field.name not in self.rel_fields:
722
+ elif field.has_default() and field.name not in self.rel_fields:
711
723
  if callable(field.default):
712
724
  return field.default()
713
725
  return field.default
@@ -7,8 +7,11 @@ default_contenttypes_mapping = {}
7
7
  __all__ = ["BAKER_CONTENTTYPES", "default_contenttypes_mapping"]
8
8
 
9
9
  if BAKER_CONTENTTYPES:
10
+ from django.contrib.contenttypes.fields import GenericForeignKey
10
11
  from django.contrib.contenttypes.models import ContentType
11
12
 
12
13
  from . import random_gen
13
14
 
14
15
  default_contenttypes_mapping[ContentType] = random_gen.gen_content_type
16
+ # a small hack to generate random object for GenericForeignKey
17
+ default_contenttypes_mapping[GenericForeignKey] = random_gen.gen_content_type
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: model-bakery
3
- Version: 1.18.2
3
+ Version: 1.19.0
4
4
  Summary: Smart object creation facility for Django.
5
5
  Project-URL: Homepage, https://github.com/model-bakers/model_bakery
6
6
  Author-email: berin <bernardoxhc@gmail.com>, amureki <amureki@hey.com>
@@ -11,6 +11,7 @@ Classifier: Development Status :: 5 - Production/Stable
11
11
  Classifier: Framework :: Django
12
12
  Classifier: Framework :: Django :: 4.2
13
13
  Classifier: Framework :: Django :: 5.0
14
+ Classifier: Framework :: Django :: 5.1
14
15
  Classifier: Intended Audience :: Developers
15
16
  Classifier: License :: OSI Approved :: Apache Software License
16
17
  Classifier: Operating System :: OS Independent
@@ -1,8 +1,8 @@
1
- model_bakery/__about__.py,sha256=UGngD1th5e8ZpEKcPUfYxQaD9diK7IjChNXBj0_xMOE,23
1
+ model_bakery/__about__.py,sha256=RI6iseDSL_qMxFcIV0RKnzR3fZIvguxQK4eA_v0LVwc,23
2
2
  model_bakery/__init__.py,sha256=yY0xZUrr_mskBm07iF-MFBEiXzOuKrlMwpKWkXZs8v0,31
3
3
  model_bakery/_types.py,sha256=P0iKC5-Cnh3lyzyZs1mlCfOW31zbEZpxqWCuYonucKo,130
4
- model_bakery/baker.py,sha256=_Q2MH1LeCFVU8mcJkE_e6sZMtHBJC9A3nw0sJPkZaVk,28232
5
- model_bakery/content_types.py,sha256=3POJ12aqPuSvCdKYuwb1GlJpSocgByG2JW5kw7VBNxQ,395
4
+ model_bakery/baker.py,sha256=bWVUdRrkmB31h8rFuTfjYeBisZjEwMTl5Ws-O99ide8,28798
5
+ model_bakery/content_types.py,sha256=O66pJZYrue0NsgKIEpVPwXb7mT-K4kEmomuFodvmZyo,613
6
6
  model_bakery/exceptions.py,sha256=q1oBZvfxL7HAD-F9aWgiYs4P4rz-hJ5TjeX7gcb3Q0I,333
7
7
  model_bakery/generators.py,sha256=agY6pH14vjehVI0wJDw3Q-u4zt7mq2M1a3CUPTkAxp0,5169
8
8
  model_bakery/gis.py,sha256=eTNOfT98_ncLAb4KmOYJhE-3ALU2_pQbUpWPTolFZJI,1028
@@ -13,7 +13,7 @@ model_bakery/random_gen.py,sha256=IYhDhqsRBftceQRHtc9B4bUWLTjvEXkgEwhNhp9DajE,97
13
13
  model_bakery/recipe.py,sha256=3PVg8y-vvfOf7Z4o9J7QPoO8eegGmejFCVWco19AWIs,8378
14
14
  model_bakery/timezone.py,sha256=rd-KGVHqJoHdHKBgENPMBTRqqxgKE91CM9C0f9nS90M,325
15
15
  model_bakery/utils.py,sha256=fw4ewtwkJ6wZ9PbDzBGvODX7CV81ozy2_6JAQmITmX4,4581
16
- model_bakery-1.18.2.dist-info/METADATA,sha256=9ipYon7s5t3EhKrKyVsBq_RMxMoWHJ0BMuH9IiLjaz0,4316
17
- model_bakery-1.18.2.dist-info/WHEEL,sha256=1yFddiXMmvYK7QYTqtRNtX66WJ0Mz8PYEiEUoOUUxRY,87
18
- model_bakery-1.18.2.dist-info/licenses/LICENSE,sha256=kCwHls7z8Y7NyZCmdnV1qLSdLrQ8bHrXLji5HOasgUc,611
19
- model_bakery-1.18.2.dist-info/RECORD,,
16
+ model_bakery-1.19.0.dist-info/METADATA,sha256=4GtPChaMl4bm1Q6ml4nNWGyaWrBqpAo88N1HRrxecFM,4355
17
+ model_bakery-1.19.0.dist-info/WHEEL,sha256=1yFddiXMmvYK7QYTqtRNtX66WJ0Mz8PYEiEUoOUUxRY,87
18
+ model_bakery-1.19.0.dist-info/licenses/LICENSE,sha256=kCwHls7z8Y7NyZCmdnV1qLSdLrQ8bHrXLji5HOasgUc,611
19
+ model_bakery-1.19.0.dist-info/RECORD,,