Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save W1773ND/ebb43b6e134da0ac58f24a07e2cdbe4e to your computer and use it in GitHub Desktop.
Save W1773ND/ebb43b6e134da0ac58f24a07e2cdbe4e to your computer and use it in GitHub Desktop.

Revisions

  1. Alican Toprak revised this gist Sep 22, 2020. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions django_field_update_checker.txt
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,4 @@
    def DjangoModel(models.Model):

    class DjangoModel(models.Model):

    @classmethod
    def from_db(cls, db, field_names, values):
  2. Alican Toprak revised this gist Sep 22, 2020. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions django_field_update_checker.txt
    Original file line number Diff line number Diff line change
    @@ -22,12 +22,12 @@ def DjangoModel(models.Model):
    :return:
    """
    if hasattr(self, '_old_values'):
    if not self.id or not self._old_values:
    if not self.pk or not self._old_values:
    return True

    for field in fields:
    if getattr(self, field) != self._old_values[field]:
    return True
    return False
    return False

    return True
  3. Alican Toprak renamed this gist Jan 30, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. Alican Toprak revised this gist Jan 30, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@ def DjangoModel(models.Model):
    if self.data_changed(['street', 'street_no', 'zip_code', 'city', 'country']):
    print("one of the fields changed")

    return true if the model saved the first time and _old_values doesnt exist
    returns true if the model saved the first time and _old_values doesnt exist

    :param fields:
    :return:
  5. Alican Toprak revised this gist Jan 30, 2017. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -15,6 +15,8 @@ def DjangoModel(models.Model):
    example:
    if self.data_changed(['street', 'street_no', 'zip_code', 'city', 'country']):
    print("one of the fields changed")

    return true if the model saved the first time and _old_values doesnt exist

    :param fields:
    :return:
  6. Alican Toprak revised this gist Jan 30, 2017. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -12,6 +12,10 @@ def DjangoModel(models.Model):

    def data_changed(self, fields):
    """
    example:
    if self.data_changed(['street', 'street_no', 'zip_code', 'city', 'country']):
    print("one of the fields changed")

    :param fields:
    :return:
    """
  7. Alican Toprak revised this gist Jan 30, 2017. No changes.
  8. Alican Toprak created this gist Jan 30, 2017.
    27 changes: 27 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    def DjangoModel(models.Model):


    @classmethod
    def from_db(cls, db, field_names, values):
    instance = super().from_db(db, field_names, values)
    instance._state.adding = False
    instance._state.db = db
    instance._old_values = dict(zip(field_names, values))
    return instance


    def data_changed(self, fields):
    """
    :param fields:
    :return:
    """
    if hasattr(self, '_old_values'):
    if not self.id or not self._old_values:
    return True

    for field in fields:
    if getattr(self, field) != self._old_values[field]:
    return True
    return False

    return True