model-bakery 1.19.1__py3-none-any.whl → 1.19.2__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.19.1"
1
+ __version__ = "1.19.2"
model_bakery/baker.py CHANGED
@@ -519,14 +519,20 @@ class Baker(Generic[M]):
519
519
  auto_now_keys[k] = attrs[k]
520
520
 
521
521
  if BAKER_CONTENTTYPES and isinstance(field, GenericForeignKey):
522
- generic_foreign_keys[k] = attrs.pop(k)
522
+ generic_foreign_keys[k] = {
523
+ "value": attrs.pop(k),
524
+ "content_type_field": field.ct_field,
525
+ "object_id_field": field.fk_field,
526
+ }
523
527
 
524
528
  instance = self.model(**attrs)
529
+
530
+ self._handle_generic_foreign_keys(instance, generic_foreign_keys)
531
+
525
532
  if _commit:
526
533
  instance.save(**_save_kwargs)
527
534
  self._handle_one_to_many(instance, one_to_many_keys)
528
535
  self._handle_m2m(instance)
529
- self._handle_generic_foreign_keys(instance, generic_foreign_keys)
530
536
  self._handle_auto_now(instance, auto_now_keys)
531
537
 
532
538
  if _from_manager:
@@ -603,7 +609,7 @@ class Baker(Generic[M]):
603
609
  ]
604
610
 
605
611
  if BAKER_CONTENTTYPES:
606
- other_fields_to_skip.append(GenericRelation)
612
+ other_fields_to_skip.extend([GenericRelation, GenericForeignKey])
607
613
 
608
614
  if isinstance(field, tuple(other_fields_to_skip)):
609
615
  return True
@@ -687,8 +693,21 @@ class Baker(Generic[M]):
687
693
  make(through_model, _using=self._using, **base_kwargs)
688
694
 
689
695
  def _handle_generic_foreign_keys(self, instance: Model, attrs: Dict[str, Any]):
690
- for key, value in attrs.items():
691
- setattr(instance, key, value)
696
+ """Set content type and object id for GenericForeignKey fields."""
697
+ for field_name, data in attrs.items():
698
+ content_type_field = data["content_type_field"]
699
+ object_id_field = data["object_id_field"]
700
+ value = data["value"]
701
+ if is_iterator(value):
702
+ value = next(value)
703
+
704
+ setattr(instance, field_name, value)
705
+ setattr(
706
+ instance,
707
+ content_type_field,
708
+ contenttypes_models.ContentType.objects.get_for_model(value),
709
+ )
710
+ setattr(instance, object_id_field, value.pk)
692
711
 
693
712
  def _remote_field(
694
713
  self, field: Union[ForeignKey, OneToOneField]
@@ -7,11 +7,8 @@ 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
11
10
  from django.contrib.contenttypes.models import ContentType
12
11
 
13
12
  from . import random_gen
14
13
 
15
14
  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.19.1
3
+ Version: 1.19.2
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>
@@ -1,8 +1,8 @@
1
- model_bakery/__about__.py,sha256=RpcrQoR9OTKxJcUGch7tBAck5gXFRixMwDygqyMiSto,23
1
+ model_bakery/__about__.py,sha256=V14nqouh0fns5BAgWrw5Aw8DW-D9yIY3TtnyECKUNxM,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=J3O_ST9URyw2_eR-3BWlZNBgdaqO3FCZT875NP-BnYE,29418
5
- model_bakery/content_types.py,sha256=O66pJZYrue0NsgKIEpVPwXb7mT-K4kEmomuFodvmZyo,613
4
+ model_bakery/baker.py,sha256=NSDx3hezvEG0HpWVXJd1uJPLxhJjc2tI-ipCzMbmWvw,30140
5
+ model_bakery/content_types.py,sha256=3POJ12aqPuSvCdKYuwb1GlJpSocgByG2JW5kw7VBNxQ,395
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.19.1.dist-info/METADATA,sha256=6kn2jbIbcPJS31uebIGjcYstoqT5MTOwEKiRDbtxpE8,4355
17
- model_bakery-1.19.1.dist-info/WHEEL,sha256=1yFddiXMmvYK7QYTqtRNtX66WJ0Mz8PYEiEUoOUUxRY,87
18
- model_bakery-1.19.1.dist-info/licenses/LICENSE,sha256=kCwHls7z8Y7NyZCmdnV1qLSdLrQ8bHrXLji5HOasgUc,611
19
- model_bakery-1.19.1.dist-info/RECORD,,
16
+ model_bakery-1.19.2.dist-info/METADATA,sha256=LjZgSNHtbrNUhoAHVpEwA0dgPudklKLP9jtJMkfYo0g,4355
17
+ model_bakery-1.19.2.dist-info/WHEEL,sha256=1yFddiXMmvYK7QYTqtRNtX66WJ0Mz8PYEiEUoOUUxRY,87
18
+ model_bakery-1.19.2.dist-info/licenses/LICENSE,sha256=kCwHls7z8Y7NyZCmdnV1qLSdLrQ8bHrXLji5HOasgUc,611
19
+ model_bakery-1.19.2.dist-info/RECORD,,