Elffuss BraidHub

kikocis/auto-code-rover public

main · 1.2 KiBRawPermalink
1# modification 1
2```python
3<file>django/db/models/base.py</file>
4<original> elif getattr(self, field.attname) is None:
5 # Use pk from related object if it has been saved after
6 # an assignment.
7 setattr(self, field.attname, obj.pk)</original>
8<patched> elif getattr(self, field.attname) is None or getattr(self, field.attname) == "":
9 # Use pk from related object if it has been saved after
10 # an assignment. This includes cases where the initial value might be an empty string.
11 setattr(self, field.attname, obj.pk)</patched>
12```
13
14Explanation:
15The patch addresses the issue where the `Order` model's `product_id` field is set to an empty string instead of being updated with the `Product` model's `sku` when the `sku` is assigned after the `Product` instance is associated with the `Order`. The original code only updates the `field.attname` if it is `None`. The patched version includes an additional check for an empty string (`""`), ensuring that the foreign key field is updated correctly even if it was initially set to an empty string. This change should prevent the data loss issue described in the bug report.