from django import forms
from django.core.exceptions import ValidationError

from cyder.base.eav.models import Attribute


class AttributeFormField(forms.CharField):
    def to_python(self, value):
        try:
            return Attribute.objects.get(
                name=value)
        except Attribute.DoesNotExist:
            raise ValidationError("No such attribute")


def get_eav_form(eav_model, entity_model):
    class EAVForm(forms.ModelForm):
        def __init__(self, *args, **kwargs):
            if 'initial' not in kwargs:
                kwargs['initial'] = dict()
            kwargs['initial']['attribute'] = kwargs['instance'].attribute.name

            super(EAVForm, self).__init__(*args, **kwargs)

        entity = forms.ModelChoiceField(
            queryset=entity_model.objects.all(),
            widget=forms.HiddenInput())

        attribute = AttributeFormField()

        class Meta:
            model = eav_model
            fields = ('entity', 'attribute', 'value')

    return EAVForm

from django import forms
from django.core.exceptions import ValidationError

from cyder.base.eav.models import Attribute


class AttributeFormField(forms.CharField):
    def to_python(self, value):
        try:
            return Attribute.objects.get(
                name=value)
        except Attribute.DoesNotExist:
            raise ValidationError("No such attribute")


def get_eav_form(eav_model, entity_model):
    class EAVForm(forms.ModelForm):
        def __init__(self, *args, **kwargs):
            if 'instance' in kwargs and kwargs['instance'] is not None:
                # This is a bound form with a real instance

                if 'initial' not in kwargs:
                    kwargs['initial'] = dict()

                # Set the attribute field to the name, not the pk
                kwargs['initial']['attribute'] = \
                    kwargs['instance'].attribute.name

            super(EAVForm, self).__init__(*args, **kwargs)

        entity = forms.ModelChoiceField(
            queryset=entity_model.objects.all(),
            widget=forms.HiddenInput())

        attribute = AttributeFormField()

        class Meta:
            model = eav_model
            fields = ('entity', 'attribute', 'value')

    return EAVForm

Fix EAV creation form; fix form error bug
Fix EAV creation form; fix form error bug

  from django import forms
  from django.core.exceptions import ValidationError
  
  from cyder.base.eav.models import Attribute
  
  
  class AttributeFormField(forms.CharField):
      def to_python(self, value):
          try:
              return Attribute.objects.get(
                  name=value)
          except Attribute.DoesNotExist:
              raise ValidationError("No such attribute")
  
  
  def get_eav_form(eav_model, entity_model):
      class EAVForm(forms.ModelForm):
          def __init__(self, *args, **kwargs):
+             if 'instance' in kwargs and kwargs['instance'] is not None:
+                 # This is a bound form with a real instance
+ 
-             if 'initial' not in kwargs:
+                 if 'initial' not in kwargs:
-                 kwargs['initial'] = dict()
+                     kwargs['initial'] = dict()
-             kwargs['initial']['attribute'] = kwargs['instance'].attribute.name
+ 
+                 # Set the attribute field to the name, not the pk
+                 kwargs['initial']['attribute'] = \
+                     kwargs['instance'].attribute.name
  
              super(EAVForm, self).__init__(*args, **kwargs)
  
          entity = forms.ModelChoiceField(
              queryset=entity_model.objects.all(),
              widget=forms.HiddenInput())
  
          attribute = AttributeFormField()
  
          class Meta:
              model = eav_model
              fields = ('entity', 'attribute', 'value')
  
      return EAVForm
  
Fix EAV creation form; fix form error bug
## Code Before:
from django import forms
from django.core.exceptions import ValidationError

from cyder.base.eav.models import Attribute


class AttributeFormField(forms.CharField):
    def to_python(self, value):
        try:
            return Attribute.objects.get(
                name=value)
        except Attribute.DoesNotExist:
            raise ValidationError("No such attribute")


def get_eav_form(eav_model, entity_model):
    class EAVForm(forms.ModelForm):
        def __init__(self, *args, **kwargs):
            if 'initial' not in kwargs:
                kwargs['initial'] = dict()
            kwargs['initial']['attribute'] = kwargs['instance'].attribute.name

            super(EAVForm, self).__init__(*args, **kwargs)

        entity = forms.ModelChoiceField(
            queryset=entity_model.objects.all(),
            widget=forms.HiddenInput())

        attribute = AttributeFormField()

        class Meta:
            model = eav_model
            fields = ('entity', 'attribute', 'value')

    return EAVForm

## Instruction:
Fix EAV creation form; fix form error bug
## Code After:
from django import forms
from django.core.exceptions import ValidationError

from cyder.base.eav.models import Attribute


class AttributeFormField(forms.CharField):
    def to_python(self, value):
        try:
            return Attribute.objects.get(
                name=value)
        except Attribute.DoesNotExist:
            raise ValidationError("No such attribute")


def get_eav_form(eav_model, entity_model):
    class EAVForm(forms.ModelForm):
        def __init__(self, *args, **kwargs):
            if 'instance' in kwargs and kwargs['instance'] is not None:
                # This is a bound form with a real instance

                if 'initial' not in kwargs:
                    kwargs['initial'] = dict()

                # Set the attribute field to the name, not the pk
                kwargs['initial']['attribute'] = \
                    kwargs['instance'].attribute.name

            super(EAVForm, self).__init__(*args, **kwargs)

        entity = forms.ModelChoiceField(
            queryset=entity_model.objects.all(),
            widget=forms.HiddenInput())

        attribute = AttributeFormField()

        class Meta:
            model = eav_model
            fields = ('entity', 'attribute', 'value')

    return EAVForm
