Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forward additional arguments to contribute_to_class() to Django #605

Merged
merged 2 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ To be released
instead of `django.utils.timezone.now` - when nullable and without a default.
- Add Brazilian Portuguese translation (GH-#578)
- Don't use `post_init` signal for initialize tracker
- Make `contribute_to_class()` in `StatusField`, `MonitorField` and `SplitField`
forward additional arguments to Django

4.4.0 (2024-02-10)
------------------
Expand Down
12 changes: 6 additions & 6 deletions model_utils/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ def prepare_class(self, sender, **kwargs):
if not self.has_default():
self.default = tuple(getattr(sender, self.choices_name))[0][0] # set first as default

def contribute_to_class(self, cls, name):
def contribute_to_class(self, cls, name, *args, **kwargs):
models.signals.class_prepared.connect(self.prepare_class, sender=cls)
# we don't set the real choices until class_prepared (so we can rely on
# the STATUS class attr being available), but we need to set some dummy
# choices now so the super method will add the get_FOO_display method
self.choices = [(0, 'dummy')]
super().contribute_to_class(cls, name)
super().contribute_to_class(cls, name, *args, **kwargs)

def deconstruct(self):
name, path, args, kwargs = super().deconstruct()
Expand Down Expand Up @@ -130,10 +130,10 @@ def __init__(self, *args, **kwargs):
self.when = when
super().__init__(*args, **kwargs)

def contribute_to_class(self, cls, name):
def contribute_to_class(self, cls, name, *args, **kwargs):
self.monitor_attname = '_monitor_%s' % name
models.signals.post_init.connect(self._save_initial, sender=cls)
super().contribute_to_class(cls, name)
super().contribute_to_class(cls, name, *args, **kwargs)

def get_monitored_value(self, instance):
return getattr(instance, self.monitor)
Expand Down Expand Up @@ -249,11 +249,11 @@ def __init__(self, *args, **kwargs):
self.add_excerpt_field = not kwargs.pop('no_excerpt_field', False)
super().__init__(*args, **kwargs)

def contribute_to_class(self, cls, name):
def contribute_to_class(self, cls, name, *args, **kwargs):
if self.add_excerpt_field and not cls._meta.abstract:
excerpt_field = models.TextField(editable=False)
cls.add_to_class(_excerpt_field_name(name), excerpt_field)
super().contribute_to_class(cls, name)
super().contribute_to_class(cls, name, *args, **kwargs)
setattr(cls, self.name, SplitDescriptor(self))

def pre_save(self, model_instance, add):
Expand Down
4 changes: 2 additions & 2 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,8 @@ def __delete__(self, obj):


class CustomDescriptorField(models.IntegerField):
def contribute_to_class(self, cls, name, **kwargs):
super().contribute_to_class(cls, name, **kwargs)
def contribute_to_class(self, cls, name, *args, **kwargs):
super().contribute_to_class(cls, name, *args, **kwargs)
setattr(cls, name, StringyDescriptor(name))


Expand Down
Loading