ManyToMany, through intermediate model

75 views
Skip to first unread message

Chris Ovenden

unread,
Jul 31, 2008, 8:32:02 AM7/31/08
to django...@googlegroups.com
Hi

I am very glad the new functionality to implement a ManyToManyField
through an intermediate table (#6095) has been merged to trunk, but I
can't figure out how to display the relationship in the Admin (I know
it's not automatic).

I have the following models (simplified):

class Product(models.Model):
title = models.CharField(max_length=255)
active = models.BooleanField(default=True)
market = models.ForeignKey('Market')
publisher = models.ForeignKey('Publisher')
shopping_providers = models.ManyToManyField('ShoppingProvider',
through='ProductShoppingProvider')

class ShoppingProvider(models.Model):
name = models.CharField(max_length=255)
url = models.URLField()
provider_margin = models.DecimalField(max_digits=4, decimal_places=2)

class ProductShoppingProvider(models.Model):
product = models.ForeignKey(Product)
shopping_provider = models.ForeignKey(ShoppingProvider)
code = models.CharField(max_length=25)
price = models.DecimalField('Cached Price', max_digits=6, decimal_places=2)

ie there could be many providers for each product (and vice versa),
but each relationship has specific info, such as price.

In the admin for products I would like the user to be able to pick
providers for each product, and enter associated price and code info.

As a first attempt I have created in my admin.py the following:

class ProductShoppingProviderInline(admin.TabularInline):
model = ProductShoppingProvider
extra = 1

class ProductOptions(admin.ModelAdmin):
inlines = ['ProductShoppingProviderInline']

content_admin = admin.AdminSite()
content_admin.register(Product, ProductOptions)

but when I visit the 'add product' page, I get the following exception:

ImproperlyConfigured at /admin/content/product/add/
Error while importing URLconf 'urls': issubclass() arg 1 must be a class


Am I doing something wrong, or is this a bug? I should mention that I
am using the latest trunk version of django-multilingual in my
project. The full product model has translated fields, although as the
error persists if I remove them, I don't think that's the problem.

Any help would be appreciated! Thanks,

Chris

Joshua Uziel

unread,
Jul 31, 2008, 9:21:38 AM7/31/08
to django...@googlegroups.com
Looks right to me, except for maybe...


class ProductOptions(admin.ModelAdmin):
   inlines = ['ProductShoppingProviderInline']

What happens if you remove the quotes from that?

Russell Keith-Magee

unread,
Jul 31, 2008, 9:26:31 AM7/31/08
to django...@googlegroups.com
On Thu, Jul 31, 2008 at 9:21 PM, Joshua Uziel <u...@ck12.org> wrote:
> Looks right to me, except for maybe...
>
> class ProductOptions(admin.ModelAdmin):
> inlines = ['ProductShoppingProviderInline']
> What happens if you remove the quotes from that?

What happens? It works :-)

Unlike ForeignKey and M2M fields which sometimes need to be able to
forward reference to a model that hasn't been defined yet, Inline
definitions can always be defined before the ModelAdmin definition
that requires them. Hence, the string notation isn't supported.

Yours,
Russ Magee %-)

Chris Ovenden

unread,
Jul 31, 2008, 10:29:35 AM7/31/08
to django...@googlegroups.com

Thanks, both of you. Getting rid of the quotes made the error go away,
but in order to see the providers inline, I had to add an inline for
the 'end' model as well:

class ShoppingProviderInline(admin.TabularInline):
model = ShoppingProvider
extra = 1

class ProductOptions(admin.ModelAdmin):
inlines = [ShoppingProviderInline, ProductShoppingProviderInline

It seems it need both the target model and the intermediate to have inlines.

But now I have just what I wanted, so thanks!

Chris

Reply all
Reply to author
Forward
0 new messages