Code

Opened 3 years ago

Closed 3 years ago

#15288 closed (worksforme)

ManyToManyField doesn't highlight currently selected items in Admin where target is a proxy object.

Reported by: anonymous Owned by: nobody
Component: Forms Version: 1.2
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

I've implemented my own single-table inheritance by creating a base class with all the fields I need, plus a 'type' field to contain the subclass objects should be instantiated as.

I've got two subclasses which are proxies for the main class, each with a custom model manager to filter results by the appropriate class.

This works almost everywhere except in the admin area. I've got a ManyToMany relationship defined on a class that references one of these proxy objects.

When I select some of these proxy objects in the admin and save the form, the database is correctly updated, but the select form widget doesn't highlight the selected items.

This affects at least django 1.2.3 to 1.2.4.

Attachments (0)

Change History (1)

comment:1 Changed 3 years ago by julien

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to worksforme
  • Status changed from new to closed

I tried to follow your instructions and wrote the following test case, but everything seems to work as expected (i.e. the saved options are highlighted in the M2M widget):

Models:

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

class Place(models.Model):
    name = models.CharField(max_length=100, blank=True)
    type = models.CharField(max_length=100, choices=(('r', 'Restaurant'), ('s', 'Shop')), blank=True)
    
    def __unicode__(self):
        return self.name
    
class RestaurantManager(models.Manager):
    def get_query_set(self):
        return super(RestaurantManager, self).get_query_set().filter(type='r')

class ShopManager(models.Manager):
    def get_query_set(self):
        return super(ShopManager, self).get_query_set().filter(type='s')

class Restaurant(Place):
    objects = RestaurantManager()
    
    class Meta:
        proxy = True
        
class Shop(Place):
    objects = ShopManager()
    
    class Meta:
        proxy = True
        
class FavoriteRestaurants(models.Model):
    user = models.ForeignKey(User)
    restaurants = models.ManyToManyField(Restaurant)

Admin:

from django.contrib import admin

from models import Restaurant, Shop, FavoriteRestaurants

admin.site.register(Restaurant)
admin.site.register(Shop)
admin.site.register(FavoriteRestaurants)

If I've missed something, then please reopen this ticket and please also provide a detailed test case.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.