Skip to content

Conversation

@shemigon
Copy link

@shemigon shemigon commented May 2, 2025

Problem

If a tracked model has a field called instance, creating a instance of the model throws

TypeError: Model.__init__() got multiple values for argument 'instance'

See issue #633

Solution

Removed instance argument from the new model init method and used args[0] instead.

Commandments

  • Write PEP8 compliant code.
  • Cover it with tests.
  • Update CHANGES.rst file to describe the changes, and quote according issue with GH-<issue_number>.
  • Pay attention to backward compatibility, or if it breaks it, explain why.
  • Update documentation (if relevant).

@livenson
Copy link

Hi, are there any plans to get this change into the upstream? We are being hit with it and wondering about prospective.

@shemigon
Copy link
Author

@livenson I'm not a maintainer of the repository. I created this PR a while ago and it's been unnoticed ever since.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes issue #633 where models with a field named "instance" would fail with a TypeError when using FieldTracker or ModelTracker. The fix changes the patched __init__ wrapper to use *args, **kwargs instead of explicitly naming the first parameter instance, avoiding conflicts with model field names.

Key Changes:

  • Modified patch_init in model_utils/tracker.py to use positional arguments instead of named parameters
  • Added test coverage for models with special field names that could conflict with internal parameter names
  • Improved timezone handling in timestamp and timeframe model tests by replacing datetime.today() with django.utils.timezone.now()

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
model_utils/tracker.py Fixed the patch_init method to avoid naming conflicts by using *args and accessing the instance as args[0]
tests/models.py Added TrackedModelWithSpecialNamedField test model with an "instance" field to verify the fix
tests/test_models/test_special_name_field_model.py New test file verifying that models with "instance" fields can be created, updated, and deleted
tests/test_models/test_timestamped_model.py Replaced datetime.today() with timezone-aware now() for proper timezone handling
tests/test_models/test_timeframed_model.py Replaced datetime.now() with timezone-aware now() for consistency
AUTHORS.rst Added contributor Boris Shemigon

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +9 to +19
def test_model_with_instance_field(self) -> None:
t = TrackedModelWithSpecialNamedField.objects.create(
instance="45.55",
name="test",
)
self.assertEqual(t.instance, "45.55")

t.instance = "56.78"
t.save()

t.delete()
Copy link

Copilot AI Dec 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test should verify that the tracker functionality works correctly for models with an "instance" field. Consider adding assertions to check tracker methods like tracker.has_changed() and tracker.previous() to ensure the fix doesn't break tracker behavior. For example, after updating t.instance = "56.78", verify that t.tracker.has_changed('instance') returns True and t.tracker.previous('instance') returns the old value.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants