Fixed #23915 -- Made sure m2m fields through non-pk to_field are allowed in the admin.
refs #23754, #23862
This commit is contained in:
parent
f9c4e14aec
commit
3a9aa155e2
|
@ -459,6 +459,12 @@ class BaseModelAdmin(six.with_metaclass(forms.MediaDefiningClass)):
|
|||
if field.primary_key:
|
||||
return True
|
||||
|
||||
# Allow reverse relationships to models defining m2m fields if they
|
||||
# target the specified field.
|
||||
for many_to_many in opts.many_to_many:
|
||||
if many_to_many.m2m_target_field_name() == to_field:
|
||||
return True
|
||||
|
||||
# Make sure at least one of the models registered for this site
|
||||
# references this field through a FK or a M2M relationship.
|
||||
registered_models = set()
|
||||
|
@ -467,7 +473,8 @@ class BaseModelAdmin(six.with_metaclass(forms.MediaDefiningClass)):
|
|||
for inline in admin.inlines:
|
||||
registered_models.add(inline.model)
|
||||
|
||||
for related_object in opts.get_all_related_objects(include_hidden=True):
|
||||
for related_object in (opts.get_all_related_objects(include_hidden=True) +
|
||||
opts.get_all_related_many_to_many_objects()):
|
||||
related_model = related_object.model
|
||||
if (any(issubclass(model, related_model) for model in registered_models) and
|
||||
related_object.field.rel.get_related_field() == field):
|
||||
|
|
|
@ -855,13 +855,19 @@ class InlineReferer(models.Model):
|
|||
refs = models.ManyToManyField(InlineReference)
|
||||
|
||||
|
||||
# Models for #23604
|
||||
# Models for #23604 and #23915
|
||||
class Recipe(models.Model):
|
||||
pass
|
||||
rname = models.CharField(max_length=20, unique=True)
|
||||
|
||||
|
||||
class Ingredient(models.Model):
|
||||
recipes = models.ManyToManyField(Recipe)
|
||||
iname = models.CharField(max_length=20, unique=True)
|
||||
recipes = models.ManyToManyField(Recipe, through='RecipeIngredient')
|
||||
|
||||
|
||||
class RecipeIngredient(models.Model):
|
||||
ingredient = models.ForeignKey(Ingredient, to_field='iname')
|
||||
recipe = models.ForeignKey(Recipe, to_field='rname')
|
||||
|
||||
|
||||
# Model for #23839
|
||||
|
|
|
@ -616,16 +616,12 @@ class AdminViewBasicTest(AdminViewBasicTestCase):
|
|||
response = self.client.get("/test_admin/admin/admin_views/notreferenced/", {TO_FIELD_VAR: 'id'})
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
# Specifying a field referenced by another model though a m2m should be allowed.
|
||||
# XXX: We're not testing against a non-primary key field since the admin doesn't
|
||||
# support it yet, ref #23862
|
||||
response = self.client.get("/test_admin/admin/admin_views/recipe/", {TO_FIELD_VAR: 'id'})
|
||||
# #23915 - Specifying a field referenced by another model though a m2m should be allowed.
|
||||
response = self.client.get("/test_admin/admin/admin_views/recipe/", {TO_FIELD_VAR: 'rname'})
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
# #23604 - Specifying a field referenced through a reverse m2m relationship should be allowed.
|
||||
# XXX: We're not testing against a non-primary key field since the admin doesn't
|
||||
# support it yet, ref #23862
|
||||
response = self.client.get("/test_admin/admin/admin_views/ingredient/", {TO_FIELD_VAR: 'id'})
|
||||
# #23604, #23915 - Specifying a field referenced through a reverse m2m relationship should be allowed.
|
||||
response = self.client.get("/test_admin/admin/admin_views/ingredient/", {TO_FIELD_VAR: 'iname'})
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
# #23329 - Specifying a field that is not referred by any other model directly registered
|
||||
|
|
Loading…
Reference in New Issue