Sunday, August 5, 2012

Re: How to override an attribute?

Agreeing with Thomas. But for the archives some background on django
inheritance:

On 5-8-2012 7:53, . wrote:

> I've read a doc page on inheritance but I don't know how to fix these errors.

In python you can override any attribute or method.
In django being a python application obviously the same applies, however
django customizes class creation for models and tucks away the field
definitions in the meta class. It therefore does not allow you to
override model fields by raising exceptions at class creation.
Django has to do this, so you or a model form can assign a value to a
model attribute and django still knows what the field definition is so
it can validate and save it. So for a model instance of a 'Person' model:
bob.name = 'Bob Jones'
bob.save()
what happens:
for field in bob._meta.fields : # that's where the field defs end up
if field.name == 'name' :
field.value = 'Bob Jones'

You *can* however implement a model field on a model class that has a
property with the same name. For example:

class Person(models.Model) :
name = models.CharField(max_length=32)

@property
def is_pregnant(self) :
return False
@pregnant.setter
def is_pregnant(self, value) :
pass

class Man(Person) :
pass

class Woman(Person) :
is_pregnant = models.BooleanField()

--
Melvyn Sopacua

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate