#12203: ManyToManyField with through model can't be used in admin
-------------------------------------+-------------------------------------
     Reporter:  David Gouldin        |                    Owner:  Rosana
                                     |  Rufer
         Type:  Bug                  |                   Status:  assigned
    Component:  contrib.admin        |                  Version:  dev
     Severity:  Normal               |               Resolution:
     Keywords:  M2M, admin,          |             Triage Stage:  Accepted
  through, through_fields            |
    Has patch:  1                    |      Needs documentation:  1
  Needs tests:  1                    |  Patch needs improvement:  1
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Comment (by hyperstown):

 Replying to [comment:22 Dmitry Mugtasimov]:
 > For some reason I ended up with this code:
 >
 > {{{
 > class JobTitleAdmin(admin.ModelAdmin):
 >     def formfield_for_manytomany(self, *args, **kwargs):  # pylint:
 disable=arguments-differ
 >         # TODO(dmu) MEDIUM: Remove `auto_created = True` after these
 issues are fixed:
 >         #                   https://code.djangoproject.com/ticket/12203
 and
 >         #                   https://github.com/django/django/pull/10829
 >
 >         # We trick Django here to avoid `./manage.py makemigrations`
 produce unneeded migrations
 >         JobTitleExperienceThrough._meta.auto_created = True  # pylint:
 disable=protected-access
 >         return super().formfield_for_manytomany(*args, **kwargs)
 >
 >
 > class JobTitle(models.Model):
 >     name = models.CharField(max_length=64)
 >     role = models.ForeignKey('JobRole', on_delete=models.CASCADE,
 >                              related_name='titles')
 >     experiences = models.ManyToManyField('Experience',
 through='JobTitleExperienceThrough',
 >                                          related_name='titles',
 blank=True)
 >
 >     class Meta:
 >         ordering = ('id',)
 > }}}

 I tried this solution for modified User and Group and Permission but it
 didn't work so I ended up with something like this:



 {{{
 class UserAdmin(BaseUserAdmin):
     def check(self, **kwargs):
         class ThroughModelAdminChecks(self.checks_class):
             def _check_field_spec_item(self, obj, field_name, label):
                 # TODO(dmu) MEDIUM: Remove `auto_created = True` after
 these issues are fixed:
                 #
 https://code.djangoproject.com/ticket/12203 and
                 #
 https://github.com/django/django/pull/18612
                 User.user_permissions.through._meta.auto_created = True  #
 pylint: disable=protected-access
                 User.groups.through._meta.auto_created = True  # pylint:
 disable=protected-access
                 return super()._check_field_spec_item(obj, field_name,
 label)
         return ThroughModelAdminChecks().check(self, **kwargs)


 class User(AbstractUser):

     groups = models.ManyToManyField(
         Group,
         through=GroupPermission,
         verbose_name=_("groups"),
         blank=True,
         help_text=_(
             "The groups this user belongs to. A user will get all
 permissions "
             "granted to each of their groups."
         ),
         related_name="user_set",
         related_query_name="user",
     )
     user_permissions = models.ManyToManyField(
         Permission,
         through=UserPermission,
         verbose_name=_("user permissions"),
         blank=True,
         help_text=_("Specific permissions for this user."),
         related_name="user_set",
         related_query_name="user",
     )
 }}}
-- 
Ticket URL: <https://code.djangoproject.com/ticket/12203#comment:28>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/01070194d2108d92-8eac283e-caa8-41e3-950d-6c4271d04317-000000%40eu-central-1.amazonses.com.

Reply via email to