Changes between Initial Version and Version 1 of Ticket #29568


Ignore:
Timestamp:
Jul 16, 2018, 10:45:45 AM (6 years ago)
Author:
François Dupayrat
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #29568 – Description

    initial v1  
    2525* INSERT IndirectChild (because previous UPDATE failed)
    2626
    27 I found a fix that worked for me by modifying _save_parents in django/db/models/base.py to return if something was inserted (default to fAlse if there is no parent), and force_insert if the parent was inserted (because if there parent was inserted, the child can't possibly exist).
     27I found a fix that worked for me by modifying _save_parents and save_base in django/db/models/base.py: _save_parents return if something was inserted (default to fAlse if there is no parent), and force_insert if the parent was inserted in both methods (because if there parent was inserted, the child can't possibly exist).
    2828
    2929Being a beginner, I'm really wary of breaking something. Could someone if it's something wanted by Django and check if there is obvious mistake?
    30 Here is the modified method:
     30Here is the modified method (without irrelevant code, materialized as [...]):
    3131{{{
    32     def _save_parents(self, cls, using, update_fields):
    33         """Save all the parents of cls using values from self."""
    34         meta = cls._meta
     32    def _save_parents([...]):
     33        [...]
    3534        inserted = False
    3635        for parent, field in meta.parents.items():
    37             # Make sure the link fields are synced between parent and self.
    38             if (field and getattr(self, parent._meta.pk.attname) is None and
    39                     getattr(self, field.attname) is not None):
    40                 setattr(self, parent._meta.pk.attname, getattr(self, field.attname))
     36            [...]
    4137            parent_inserted = self._save_parents(cls=parent, using=using, update_fields=update_fields)
    4238            updated = self._save_table(cls=parent, using=using, update_fields=update_fields, force_insert=parent_inserted)
    4339            if not updated:
    4440                inserted = True
    45             # Set the parent's PK value to self.
    46             if field:
    47                 setattr(self, field.attname, self._get_pk_val(parent._meta))
    48                 # Since we didn't have an instance of the parent handy set
    49                 # attname directly, bypassing the descriptor. Invalidate
    50                 # the related object cache, in case it's been accidentally
    51                 # populated. A fresh instance will be re-built from the
    52                 # database if necessary.
    53                 if field.is_cached(self):
    54                     field.delete_cached_value(self)
     41            [...]
    5542        return inserted
     43    def save_base([...]):
     44        [...]
     45        with transaction.atomic(using=using, savepoint=False):
     46            parent_inserted = False
     47            if not raw:
     48                parent_inserted = self._save_parents(cls, using, update_fields)
     49            updated = self._save_table(raw, cls, force_insert or parent_inserted, force_update, using, update_fields)
     50        [...]
    5651}}}
Back to Top