1

I'm using Satchmo and Django and am trying to extend Satchmo's Product model. I'd like to make one of the fields in Satchmo's Product model have a default value in the admin without changing Satchmo's source code. Here is an abbreviated version of Satchmo's Product model:

class Product(models.Model):
    site = models.ForeignKey(Site, verbose_name='Site')

This is what I attempted to do to extend it...

class MyProduct(Product):
    Product.site = models.ForeignKey(Site, verbose_name='Site', editable=False, default=1)

This does not work, any ideas on why?

Udbhav
  • 353
  • 1
  • 3
  • 10

3 Answers3

1

For two reasons, firstly the way you are trying to override a class variable just isn't how it works in Python. You just define it in the class as normal, the same way that def __init__(self): is overriding the super-class initializer. But, Django model inheritance simply doesn't support this. If you want to add constraints, you could do so in the save() method.

ironfroggy
  • 7,991
  • 7
  • 33
  • 44
1

You could probably monkeypatch it if you really wanted to:

site_field = Product._meta.get_field('site')
site_field.editable = False
site_field.default = 1

But this is a nasty habit and could cause problems; arguably less maintainable than just patching Satchmo's source directly.

Carl Meyer
  • 122,012
  • 20
  • 106
  • 116
-2

You can't change the superclass from a subclass.

You have the source. Use subversion. Make the change. When Satchmo is updated merge the updates around your change.

S.Lott
  • 384,516
  • 81
  • 508
  • 779