model-bakery 1.16.0__tar.gz → 1.17.0__tar.gz

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.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: model-bakery
3
- Version: 1.16.0
3
+ Version: 1.17.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>
@@ -55,7 +55,7 @@ Model Bakery is a rename of the legacy [Model Mommy project](https://pypi.org/pr
55
55
  ## Install
56
56
 
57
57
  ```bash
58
- pip install model_bakery
58
+ pip install model-bakery
59
59
  ```
60
60
 
61
61
  ## Usage and Info
@@ -15,7 +15,7 @@ Model Bakery is a rename of the legacy [Model Mommy project](https://pypi.org/pr
15
15
  ## Install
16
16
 
17
17
  ```bash
18
- pip install model_bakery
18
+ pip install model-bakery
19
19
  ```
20
20
 
21
21
  ## Usage and Info
@@ -0,0 +1 @@
1
+ __version__ = "1.17.0"
@@ -498,17 +498,30 @@ class Baker(Generic[M]):
498
498
  self, attrs: Dict[str, Any], _commit, _save_kwargs, _from_manager
499
499
  ) -> M:
500
500
  one_to_many_keys = {}
501
+ auto_now_keys = {}
502
+
501
503
  for k in tuple(attrs.keys()):
502
504
  field = getattr(self.model, k, None)
505
+
506
+ if not field:
507
+ continue
508
+
503
509
  if isinstance(field, ForeignRelatedObjectsDescriptor):
504
510
  one_to_many_keys[k] = attrs.pop(k)
505
511
 
512
+ if hasattr(field, "field") and (
513
+ getattr(field.field, "auto_now_add", False)
514
+ or getattr(field.field, "auto_now", False)
515
+ ):
516
+ auto_now_keys[k] = attrs[k]
517
+
506
518
  instance = self.model(**attrs)
507
519
  # m2m only works for persisted instances
508
520
  if _commit:
509
521
  instance.save(**_save_kwargs)
510
522
  self._handle_one_to_many(instance, one_to_many_keys)
511
523
  self._handle_m2m(instance)
524
+ self._handle_auto_now(instance, auto_now_keys)
512
525
 
513
526
  if _from_manager:
514
527
  # Fetch the instance using the given Manager, e.g.
@@ -609,6 +622,12 @@ class Baker(Generic[M]):
609
622
 
610
623
  return False
611
624
 
625
+ def _handle_auto_now(self, instance: Model, attrs: Dict[str, Any]):
626
+ if not attrs:
627
+ return
628
+
629
+ instance.__class__.objects.filter(pk=instance.pk).update(**attrs)
630
+
612
631
  def _handle_one_to_many(self, instance: Model, attrs: Dict[str, Any]):
613
632
  for key, values in attrs.items():
614
633
  manager = getattr(instance, key)
@@ -112,9 +112,7 @@ def gen_time() -> time:
112
112
 
113
113
 
114
114
  def gen_string(max_length: int) -> str:
115
- return str(
116
- "".join(baker_random.choice(string.ascii_letters) for _ in range(max_length))
117
- )
115
+ return "".join(baker_random.choice(string.ascii_letters) for _ in range(max_length))
118
116
 
119
117
 
120
118
  def _gen_string_get_max_length(field: Field) -> Tuple[str, int]:
@@ -129,7 +127,7 @@ gen_string.required = [_gen_string_get_max_length] # type: ignore[attr-defined]
129
127
 
130
128
  def gen_slug(max_length: int) -> str:
131
129
  valid_chars = string.ascii_letters + string.digits + "_-"
132
- return str("".join(baker_random.choice(valid_chars) for _ in range(max_length)))
130
+ return "".join(baker_random.choice(valid_chars) for _ in range(max_length))
133
131
 
134
132
 
135
133
  gen_slug.required = ["max_length"] # type: ignore[attr-defined]
@@ -1 +0,0 @@
1
- __version__ = "1.16.0"
File without changes
File without changes