well override ModelForm's __init__ and if theres a kwargs , then populate 
choices depending upon the value of model_name , as well override def 
clean() to handle model attribute.

class ConditionSetAdminForm(forms.ModelForm):
    def __init__(self,*args,**kwargs):
        super(ConditionSetAdminForm, self).__init__(*args,**kwargs)
        try:
            model_name = kwargs['instance'].model_name
            app_label,model_class = 
model_name.split('.')[0],model_name.split('.')[1] 
            model_class = get_model(app_label=app_label, 
model_name=model_class)
            self.fields['model_attribute']  = 
forms.ChoiceField(required=True, label='Model Attribute', choices= 
[(each.name,each.name) for each in model_class._meta.fields])
        except:
            self.fields['model_attribute']  = 
forms.ChoiceField(required=True, label='Model Attribute', 
choices=get_attributes())
        self.fields['model_name']       = forms.ChoiceField(required=True, 
label='Model Name', choices=get_all_models())        
    class Meta: 
        model = ConditionSet
    def clean(self):
        super(ConditionSetAdminForm,self).clean()
        if 'model_attribute' in self._errors:
            self.cleaned_data['model_attribute'] = 
self.data['model_attribute']
            del self._errors['model_attribute']
        return self.cleaned_data

just like that ;)

On Tuesday, February 12, 2013 5:09:51 PM UTC+5:30, vijay shanker wrote:
>
> hey all
>
> I have a model which is as such:
> *in models.py*
>
> class ConditionSet(models.Model):
>     model_name      =    models.CharField(max_length=
> 50) 
>     model_attribute  =    models.CharField(max_length=50)
>     operator             =    models.CharField(max_length=50, 
> choices=OPERATORS)
>     val                    =    
> models.CharField(max_length=50,null=True,blank=True)
>     evaluation          =    models.NullBooleanField(null=True, blank=True)
>     def __str__(self):
>         return self.model_attribute
>
> *in admin.py :*
>
> def get_all_models():
>     return [('.'.join([each._meta.app_label,each.__name__]),each.__name__) 
> for each in [each.model_class() for each in 
> ContentType.objects.filter(app_label__in=['products','productoptions','shoppingcart'])
>  
> if each is not None] if each is not None]    
>
> def get_attributes():
>     model_class = 
> ContentType.objects.filter(app_label__in=['products','productoptions','shoppingcart'])[0].model_class()
>     return [ (each.name,each.name) for each in model_class._meta.fields ]
>
> class ConditionSetAdminForm(forms.ModelForm):
>     def __init__(self,*args,**kwargs):
>         super(ConditionSetAdminForm, self).__init__(*args,**kwargs)
>         self.fields['model_name']       = forms.ChoiceField(required=True, 
> label='Model Name', choices=get_all_models())
>         self.fields['model_attribute']  = forms.ChoiceField(required=True, 
> label='Model Attribute', choices=get_attributes())
>     class Meta: 
>         model = ConditionSet    
>
> class ConditionSetAdmin(admin.ModelAdmin):
>     form = ConditionSetAdminForm
>
> admin.site.register(ConditionSet, ConditionSetAdmin )
>
> model_attribute has an initial set of choices, populated by 
> get_attributes().
> When a user selects model_name in admin, i make a json call to get options 
> and populate model_attribute options , but when i try to save it, i get  
> "select a valid choice. xyz_attribute_name is not one of the available 
> choices."
> I understand the problem is the choices for this field are still the one 
> assigned to it in beginning, how should I fix it ?
>
>

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


Reply via email to