Opened 2 years ago

Closed 2 years ago

#21593 closed Bug (duplicate)

Can't restrict formfield_for_manytomany queryset if the m2m field is django.contrib.auth.models.User

Reported by: segfault Owned by: nobody
Component: contrib.admin Version: 1.6
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


This is an example showing the odd behaviour:

from django.contrib import admin
from django.contrib.auth.models import User

from models import Article, AM2MField

class RestrictedModelAdmin(admin.ModelAdmin):
    def formfield_for_manytomany(self, db_field, request, **kwargs):
        if == 'users':
            kwargs['queryset'] = User.objects.filter(username="user1")
        if == 'am2m':
            kwargs['queryset'] = AM2MField.objects.filter(name="m1")
        return super(RestrictedModelAdmin, self).formfield_for_manytomany(db_field, request, **kwargs), RestrictedModelAdmin)

from django.db import models
from django.contrib.auth.models import User

class AM2MField(models.Model):
    name = models.CharField(max_length=255)

class Article(models.Model):
    am2m = models.ManyToManyField(AM2MField)
    users = models.ManyToManyField(User)

The Users field is showing all of the system users, ignoring the filtering.

Change History (2)

comment:1 Changed 2 years ago by segfault

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

The bug seems to be fixed in the master version.

comment:2 Changed 2 years ago by charettes

  • Resolution set to duplicate
  • Status changed from new to closed

This is a duplicate of #21405. The fix was backported to the 1.6 branch and should be part of the 1.6.1 release.

Note: See TracTickets for help on using tickets.
Back to Top