Changes between Version 1 and Version 2 of Ticket #12203, comment 28


Ignore:
Timestamp:
Feb 4, 2025, 12:37:41 PM (8 days ago)
Author:
hyperstown

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #12203, comment 28

    v1 v2  
    2525> }}}
    2626
    27 I tried this solution for modified User and Group and Permission but it didn't work so I ended up with something like this:
     27I tried this solution for modified User and Group and Permission but it didn't work, I needed to add auto_created=True in time of checks as well. I also needed to set it back to false after checks or migrations would actually pick it up same as set in the model.
    2828
    29 
     29The code I ended up with is something like this:
    3030
    3131{{{
    3232class UserAdmin(BaseUserAdmin):
     33
     34    # TODO(dmu) MEDIUM: Remove `auto_created = True` after these issues are fixed:
     35    #                               https://code.djangoproject.com/ticket/12203 and
     36    #                               https://github.com/django/django/pull/18612
     37    def formfield_for_manytomany(self, *args, **kwargs):
     38        User.groups.through._meta.auto_created = True  # pylint: disable=protected-access
     39        User.user_permissions.through._meta.auto_created = True  # pylint: disable=protected-access
     40        field = super().formfield_for_manytomany(*args, **kwargs)
     41        User.user_permissions.through._meta.auto_created = False  # pylint: disable=protected-access
     42        User.groups.through._meta.auto_created = False  # pylint: disable=protected-access
     43        return field
     44
    3345    def check(self, **kwargs):
    3446        class ThroughModelAdminChecks(self.checks_class):
    3547            def _check_field_spec_item(self, obj, field_name, label):
    36                 # TODO(dmu) MEDIUM: Remove `auto_created = True` after these issues are fixed:
    37                 #                               https://code.djangoproject.com/ticket/12203 and
    38                 #                               https://github.com/django/django/pull/18612
    3948                User.user_permissions.through._meta.auto_created = True  # pylint: disable=protected-access
    4049                User.groups.through._meta.auto_created = True  # pylint: disable=protected-access
     
    4352                User.groups.through._meta.auto_created = False  # pylint: disable=protected-access
    4453                return result
     54
    4555        return ThroughModelAdminChecks().check(self, **kwargs)
    4656
Back to Top