Opened 14 years ago

Closed 14 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: no UI/UX: no

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.

Change History (1)

comment:1 by Julien Phalip, 14 years ago

Resolution: worksforme
Status: newclosed

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.

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