views:

229

answers:

4

I'm looking to do this:

class Place(models.Model):
 name = models.CharField(max_length=20)
 rating = models.DecimalField()

class LongNamedRestaurant(Place):  # Subclassing `Place`.
 name = models.CharField(max_length=255)  # Notice, I'm overriding `Place.name` to give it a longer length.
 food_type = models.CharField(max_length=25)

This is the version I would like to use (although I'm open to any suggestion): http://docs.djangoproject.com/en/dev/topics/db/models/#id7

Is this supported in Django? If not, is there a way to achieve similar results?

A: 

Django is Python. So yes.

myfreeweb
It might also be safe to assume the poster isn't as naive as you'd like them to be and are in fact asking if Django supports subclassed models overriding field attributes in the parent model.
Brian Luft
@Brian I actually know that Django doesn't support this. I asked it in a naive way because people tend to be more helpful if they think they're solving a problem. I was looking for somebody to who maybe found a way around this annoying problem with the ORM.
Johnny 5
+1  A: 

Pasted your code into a fresh app, added app to INSTALLED_APPS and ran syncdb:

django.core.exceptions.FieldError: Local field 'name' in class 'LongNamedRestaurant' clashes with field of similar name from base class 'Place'

Looks like Django does not support that.

Brian Luft
A: 

Maybe you could deal with contribute_to_class :

class LongNamedRestaurant(Place):

    food_type = models.CharField(max_length=25)

    def __init__(self, *args, **kwargs):
        super(self, LongNamedRestaurant).__init__(*args, **kwargs)
        name = models.CharField(max_length=255)
        name.contribute_to_class('name', self)

Syncdb works fine. I dont tried this example, in my case I just override a constraint parameter so ... wait & see !

JF Simon