# Path: hstore_flattenfields/models.py
# class DynamicField(models.Model):
#     """
#     Created to represent the Django Model's field information,
#     we use him to fill the Field instances when the ``refer``
#     instances will be build.
# 
#     :param refer: The name of the Model of the ``DynamicField``.
#     :param name: The name of the ``DynamicField``.
#     :param verbose_name: The Verbose Name of the ``DynamicField``.
#     :param order: The 0-indexed order which the ``DynamicField`` is places inside the ``Form``.
#     :param content_type: The ``ContentType`` which the ``ContentPane`` will be shown.
#     :param group: The ``DynamicFieldGroup`` instance which the ``ContentPane`` is owned.
# 
#     """
#     refer = models.CharField(max_length=120, blank=False, db_index=True, verbose_name=_("Class name"))
#     name = models.CharField(max_length=120, blank=False, db_index=True,unique=True, verbose_name=_("Field name"))
#     verbose_name = models.CharField(max_length=120, blank=False, verbose_name=_("Verbose name"))
#     typo = models.CharField(max_length=20, blank=False, db_index=True, verbose_name=_("Field type"), choices=single_list_to_tuple(FIELD_TYPES))
#     max_length = models.IntegerField(null=True, blank=True, verbose_name=_("Length"))
#     order = models.IntegerField(null=True, blank=True, default=None, verbose_name=_("Order"))
#     blank = models.BooleanField(default=True, verbose_name=_("Blank"))
#     choices = models.TextField(null=True, blank=True, verbose_name=_("Choices"))
#     default_value = models.CharField(max_length=80, null=True, blank=True, verbose_name=_("Default value"))
#     help_text = models.CharField(max_length=255, null=True, blank=True, verbose_name=_('Help Text'))
#     html_attrs = hstore.DictionaryField(db_index=True, null=True, blank=True, default=None, verbose_name=_("html Attributes"))
# 
#     # relations
#     group = models.ForeignKey(DynamicFieldGroup, null=True, blank=True, related_name="dynamic_fields", verbose_name=_("Groups"))
#     content_pane = models.ForeignKey(ContentPane, null=True, blank=True, related_name="dynamic_fields", verbose_name=_("Panel"))
# 
#     class Meta:
#         verbose_name = _('Dynamic Field')
#         verbose_name_plural = _('Dynamic Fields')
#         # ordering = ['pk']
# 
#     def __unicode__(self):
#         return self.verbose_name or self.name
# 
#     @property
#     def has_blank_option(self):
#         return self.blank and self.typo not in FIELD_TYPES_WITHOUT_BLANK_OPTION


from django import forms
from django.db import models
from django.utils.text import capfirst
from decimal import Decimal, InvalidOperation
from datetime import date, datetime
from hstore_flattenfields.utils import *
from hstore_flattenfields.forms.fields import *
from hstore_flattenfields.models import DynamicField
        Returns a django.forms.Field instance for this database Field.
        """
        defaults = {
            'required': not self.blank,
            'label': capfirst(self.verbose_name),
            'help_text': self.help_text,
            'html_attrs': self.html_attrs
        }

        if self.has_default():
            defaults['initial'] = self.default

        if self.choices:
            # Fields with choices get special treatment.
            include_blank = (self.blank or
                             not (self.has_default() or 'initial' in kwargs))
            defaults['choices'] = self.get_choices(include_blank=include_blank)
            defaults['coerce'] = self.to_python
            if self.null:
                defaults['empty_value'] = ""
        defaults.update(kwargs)
        return form_class(**defaults)

    def to_python(self, value):
        if value is models.fields.NOT_PROVIDED or value is None:
            return None
        else:
            return value

    def get_choices(self, include_blank=False):

# Path: hstore_flattenfields/models.py
# class DynamicField(models.Model):
#     """
#     Created to represent the Django Model's field information,
#     we use him to fill the Field instances when the ``refer``
#     instances will be build.
# 
#     :param refer: The name of the Model of the ``DynamicField``.
#     :param name: The name of the ``DynamicField``.
#     :param verbose_name: The Verbose Name of the ``DynamicField``.
#     :param order: The 0-indexed order which the ``DynamicField`` is places inside the ``Form``.
#     :param content_type: The ``ContentType`` which the ``ContentPane`` will be shown.
#     :param group: The ``DynamicFieldGroup`` instance which the ``ContentPane`` is owned.
# 
#     """
#     refer = models.CharField(max_length=120, blank=False, db_index=True, verbose_name=_("Class name"))
#     name = models.CharField(max_length=120, blank=False, db_index=True,unique=True, verbose_name=_("Field name"))
#     verbose_name = models.CharField(max_length=120, blank=False, verbose_name=_("Verbose name"))
#     typo = models.CharField(max_length=20, blank=False, db_index=True, verbose_name=_("Field type"), choices=single_list_to_tuple(FIELD_TYPES))
#     max_length = models.IntegerField(null=True, blank=True, verbose_name=_("Length"))
#     order = models.IntegerField(null=True, blank=True, default=None, verbose_name=_("Order"))
#     blank = models.BooleanField(default=True, verbose_name=_("Blank"))
#     choices = models.TextField(null=True, blank=True, verbose_name=_("Choices"))
#     default_value = models.CharField(max_length=80, null=True, blank=True, verbose_name=_("Default value"))
#     help_text = models.CharField(max_length=255, null=True, blank=True, verbose_name=_('Help Text'))
#     html_attrs = hstore.DictionaryField(db_index=True, null=True, blank=True, default=None, verbose_name=_("html Attributes"))
# 
#     # relations
#     group = models.ForeignKey(DynamicFieldGroup, null=True, blank=True, related_name="dynamic_fields", verbose_name=_("Groups"))
#     content_pane = models.ForeignKey(ContentPane, null=True, blank=True, related_name="dynamic_fields", verbose_name=_("Panel"))
# 
#     class Meta:
#         verbose_name = _('Dynamic Field')
#         verbose_name_plural = _('Dynamic Fields')
#         # ordering = ['pk']
# 
#     def __unicode__(self):
#         return self.verbose_name or self.name
# 
#     @property
#     def has_blank_option(self):
#         return self.blank and self.typo not in FIELD_TYPES_WITHOUT_BLANK_OPTION


# Path: hstore_flattenfields/db/fields.py
from django import forms
from django.db import models
from django.utils.text import capfirst
from decimal import Decimal, InvalidOperation
from datetime import date, datetime
from hstore_flattenfields.utils import *
from hstore_flattenfields.forms.fields import *
from hstore_flattenfields.models import DynamicField

        Returns a django.forms.Field instance for this database Field.
        """
        defaults = {
            'required': not self.blank,
            'label': capfirst(self.verbose_name),
            'help_text': self.help_text,
            'html_attrs': self.html_attrs
        }

        if self.has_default():
            defaults['initial'] = self.default

        if self.choices:
            # Fields with choices get special treatment.
            include_blank = (self.blank or
                             not (self.has_default() or 'initial' in kwargs))
            defaults['choices'] = self.get_choices(include_blank=include_blank)
            defaults['coerce'] = self.to_python
            if self.null:
                defaults['empty_value'] = ""
        defaults.update(kwargs)
        return form_class(**defaults)

    def to_python(self, value):
        if value is models.fields.NOT_PROVIDED or value is None:
            return None
        else:
            return value

    def get_choices(self, include_blank=False):

        dynamic_field = DynamicField.objects.get(name=self.name)