From 3c4699529118745595ac7aec815f557558e645ba Mon Sep 17 00:00:00 2001 From: georgemillard Date: Mon, 26 Jun 2017 10:43:37 +0100 Subject: [PATCH 1/2] Update Docs - utilities.rst based on issue #195 Having encountered this issue a couple of times with FieldTrackers, I thought it would be good to go in the docs! Hope this helps. --- docs/utilities.rst | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/docs/utilities.rst b/docs/utilities.rst index 44824f5..6fcd6b4 100644 --- a/docs/utilities.rst +++ b/docs/utilities.rst @@ -212,6 +212,50 @@ An example using the model specified above: {'title': None} +Tracking Foreign Key Fields +--------------------------- + +It should be noted that a generic FieldTracker tracks Foreign Keys by db_column name, rather than model field name, and would be accessed as follows: + +.. code-block:: python + + from django.db import models + from model_utils import FieldTracker + + class Parent(models.Model): + name = models.CharField(max_length=64) + + class Child(models.Model): + name = models.CharField(max_length=64) + parent = models.ForeignKey(Parent) + +.. code-block:: pycon + + >>> p = Parent.objects.create(name='P') + >>> c = Child.objects.create(name='C', parent=p) + >>> c.tracker.has_changed('parent_id') + + +To find the db_column names of your model (using the above example): + +.. code-block:: pycon + + >>> for field in Child._meta.fields: + field.get_attname_column() + ('id', 'id') + ('name', 'name') + ('parent_id', 'parent_id') + + +The model field name *may* be used when tracking with a specific tracker: + +.. code-block:: python + + specific_tracker = FieldTracker(fields=['parent']) + +But according to issue #195 this is not recommended for accessing Foreign Key Fields. + + Checking changes using signals ------------------------------ From 39bd66d1203331e38d1fd0e9d5020cfc6d34c11a Mon Sep 17 00:00:00 2001 From: georgemillard Date: Tue, 20 Aug 2019 16:01:15 +0100 Subject: [PATCH 2/2] Added tracker field to the Child model definition --- docs/utilities.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/utilities.rst b/docs/utilities.rst index 6fcd6b4..926e8ac 100644 --- a/docs/utilities.rst +++ b/docs/utilities.rst @@ -228,6 +228,7 @@ It should be noted that a generic FieldTracker tracks Foreign Keys by db_column class Child(models.Model): name = models.CharField(max_length=64) parent = models.ForeignKey(Parent) + tracker = FieldTracker() .. code-block:: pycon