Closed xjlin0 closed 3 months ago
When I copying the example pointing auth.User, makemigration complains:
It sounds like I need to update the docs to use settings.AUTH_USER_MODEL
instead of "auth.User"
. That would prevent this particular warning from happening
makemigrations, migrate, createsuperuser and seeding data seems fine, however, it complains ValueError: Must use .target() to target an object for event aggregation in the django shell:
The aggregate event model only works across one object at a time right now, hence why it forces you to use target()
to target an object. Can you elaborate on what you were trying to achieve?
Hi Wesley, I am wondering if I can use aggregate event as a "master document collection". For example there are three classes: People, Address and Contact. When app user changes a people's contact or address, the app user expects to see "User" history changes too, although the change was only on the the contact or address level. Just like when I change telephone number in my credit card app, I have no idea what's the class behind, and I will think "my data changed".
To deal with such scenario, we can mark all three class as a "collection", and when any of the class changes, the history of the "collection" changes too, which fits app user's expectation.
Would aggregate event be function as such "collection" too? thanks!
@xjlin0 currently the .target()
method is designed to do what you described. If, for example, address and contact foreign key to people, changes to those models will be aggregated when doing .target(people_object)
. I.e. you will see snapshot models for all three models if any foreign keys to the people object are detected.
I think this describes your use case, right? You need to aggregate historical changes that reference a particular object? Currently the code only aggregates models that directly foreign key to the targeted model.
FYI I'm overhauling the aggregate event helper models to make them more flexible and easier to query. I'm planning to have a new version ready by beginning of next week. The old AggregateEvent model is going to be deprecated, and a new Events
model will be there. It currently looks like this:
class Events(models.Model):
"""
A proxy model for aggregating events together across tables and
rendering diffs
"""
# A surrogate primary key that is composed of the event table and the event PK
pgh_pk = models.TextField(primary_key=True)
# The primary key of the event table
pgh_id = models.BigIntegerField()
# When the event was created
pgh_created_at = models.DateTimeField(auto_now_add=True)
# The label of the event
pgh_label = models.TextField(help_text="The event label.")
# The table of the event model
pgh_table = models.CharField(
max_length=64, help_text="The table under which the event is stored."
)
# The raw data of the event row
pgh_data = PGHistoryJSONField(help_text="The raw data of the event row.")
# The changes between this row and the last event with the same label and object
pgh_diff = PGHistoryJSONField(
help_text="The diff between the previous event and the current event."
)
# The UUID of the context, if any, that was tracked
pgh_context_id = models.UUIDField(null=True, help_text="The ID associated with the context.")
# The context that was tracked
pgh_context = models.JSONField(
"pghistory.Context",
null=True,
help_text="The context associated with the event.",
)
# The table of the primary event object.
pgh_obj_table = models.CharField(
max_length=64, help_text="The table under which the primary object is stored."
)
# The pk of the primary event object
pgh_obj_id = models.TextField(null=True, help_text="The ID of the primary object.")
The view is pretty similar to what exists now with some additional fields. Here's some key differences:
Events.objects.all()
can be called and return all events across all tables. You are no longer required to filter on an individual event.Events.objects.across(EventModel1, EventModel2)
can be used to limit the search space to event modelsEvents.objects.tracks(primary_obj1, primary_obj2)
can be used to limit the search space to event models that directly track the provided objects.Events.objects.references(obj1, obj2)
can be used to limit the search space to event models that foreign key to the provided objects. This is the same as the .target
method from the old version, and I think it fits your use case. I can consider adding a depth
argument to extend the depth of the search and joins too.The reason I'm going with this approach in the later version is because I'm building a direct admin view on top of events to address some of the Django admin integration issues.
Anyways, didn't mean to blast you with this message, but if you have any feedback or suggestions for the aggregate event API let me know!
As a follow-up, this change was released and documented in this section a while ago.
Hi team, thanks again for creating such a great package, it's been a pleasure to develop with it. Here is an observation regarding " Retrieving and Joining AggregateEvent Metadata" in the doc.
https://django-pghistory.readthedocs.io/en/latest/extras.html#retrieving-and-joining-aggregateevent-metadata When I copying the example pointing
auth.User
, makemigration complains:Thus is my user model modifying the doc example pointing settings.AUTH_USER_MODEL, which is my User model.
makemigrations, migrate, createsuperuser and seeding data seems fine, however, it complains
ValueError: Must use .target() to target an object for event aggregation
in the django shell:Thanks for any suggestions.