mirror of
https://github.com/Hopiu/django-model-utils.git
synced 2026-03-16 20:00:23 +00:00
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.
This commit is contained in:
parent
343d459406
commit
3c46995291
1 changed files with 44 additions and 0 deletions
|
|
@ -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
|
||||
------------------------------
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue