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