Sunday, 12 May 2013

Django - Override Abstract Model Fields

models.py

class Payment(CommonModel):
"""Payment info"""
....
class Meta:
    abstract = True

class AdvancePayment(Payment):
"""Advance Payment info"""
....
AdvancePayment._meta.get_field('foo').blank = TrueAdvancePayment._meta.get_field('foo').null = True

Special case
 ? The field 'AdvancePayment.foo' does not have a default specified, yet is NOT NULL.
 ? Since you are making this field nullable, you MUST specify a default
 ? value to use for existing rows. Would you like to:
 ?  1. Quit now, and add a default to the field in models.py
 ?  2. Specify a one-off value to use for existing columns now
 ?  3. Disable the backwards migration by raising an exception.
 ? Please select a choice: 2
 ? Please enter Python code for your one-off default value.
 ? The datetime module is available, so you can do e.g. datetime.date.today()
 >>> ''

 South generated migration (Changes for backwards compatibility)

def backwards(self, orm):
....
db.alter_column('sales_advancepayment', 'foo', self.gf('django.db.models.fields.DateField')(default=''))

change to

db.alter_column('sales_advancepayment', 'foo', self.gf('django.db.models.fields.DateField')(null=False, blank=False))

No comments :

Post a Comment