From: Stephen Burrows Date: Thu, 21 Oct 2010 16:15:14 +0000 (-0400) Subject: Tweaked philo migrations to force contenttype creation after migration 0005 so the... X-Git-Tag: philo-0.9~29^2~3 X-Git-Url: http://git.ithinksw.org/philo.git/commitdiff_plain/f578580e09ad5ef0466d48983c7480fcaccb42b4 Tweaked philo migrations to force contenttype creation after migration 0005 so the contenttypes are available in migration 0006. Ordinarily contenttypes are only synced after a complete set of migrations. Also implemented default values on proxy fields; partially addresses feature #16. --- diff --git a/migrations/0005_add_attribute_values.py b/migrations/0005_add_attribute_values.py index d8675ee..9bea73f 100644 --- a/migrations/0005_add_attribute_values.py +++ b/migrations/0005_add_attribute_values.py @@ -40,6 +40,10 @@ class Migration(SchemaMigration): # Adding unique constraint on 'Attribute', fields ['value_object_id', 'value_content_type'] db.create_unique('philo_attribute', ['value_object_id', 'value_content_type_id']) + # Manual addition! This is necessary to immediately cause contenttype creation. + # (needed for the next migration) + db.send_pending_create_signals() + def backwards(self, orm): diff --git a/models/fields.py b/models/fields.py index 74a0525..cff8ff8 100644 --- a/models/fields.py +++ b/models/fields.py @@ -1,6 +1,7 @@ -from django.db import models from django import forms from django.core.exceptions import FieldError, ValidationError +from django.db import models +from django.db.models.fields import NOT_PROVIDED from django.utils import simplejson as json from django.utils.text import capfirst from philo.signals import entity_class_prepared @@ -13,11 +14,12 @@ __all__ = ('JSONAttribute', 'ForeignKeyAttribute', 'ManyToManyAttribute') class EntityProxyField(object): descriptor_class = None - def __init__(self, *args, **kwargs): + def __init__(self, verbose_name=None, help_text=None, default=NOT_PROVIDED, *args, **kwargs): if self.descriptor_class is None: raise NotImplementedError('EntityProxyField subclasses must specify a descriptor_class.') - self.verbose_name = kwargs.get('verbose_name', None) - self.help_text = kwargs.get('help_text', None) + self.verbose_name = verbose_name + self.help_text = help_text + self.default = default def actually_contribute_to_class(self, sender, **kwargs): sender._entity_meta.add_proxy_field(self) @@ -39,6 +41,9 @@ class EntityProxyField(object): def value_from_object(self, obj): return getattr(obj, self.attname) + + def has_default(self): + return self.default is not NOT_PROVIDED class AttributeFieldDescriptor(object): @@ -124,6 +129,8 @@ class JSONAttribute(AttributeField): def formfield(self, **kwargs): defaults = {'required': False, 'label': capfirst(self.verbose_name), 'help_text': self.help_text} + if self.has_default(): + defaults['initial'] = self.default defaults.update(kwargs) return self.field_template.formfield(**defaults) @@ -153,6 +160,8 @@ class ForeignKeyAttribute(AttributeField): def formfield(self, form_class=forms.ModelChoiceField, **kwargs): defaults = {'required': False, 'label': capfirst(self.verbose_name), 'help_text': self.help_text} + if self.has_default(): + defaults['initial'] = self.default defaults.update(kwargs) return form_class(self.model._default_manager.complex_filter(self.limit_choices_to), **defaults)