Hi,

What you are suggesting doesn't work. You can't communicate with the django
backend via javascript if you don't use a lot of ajax requests. I would
check django-smart-selects and see if you could use that?

Regards,

Andréas

2015-11-02 15:57 GMT+01:00 dc <[email protected]>:

> Any lead will be extremely helpful. I am still stuck. :(
>
> On Thursday, October 29, 2015 at 11:40:32 PM UTC-4, dc wrote:
>>
>> I have declared a charfield 'choice_text' in one of my models. I want to
>> convert it to a dropdown box in django admin. The choices in the dropdown
>> list depend on user input to a textbox defined in another model class. I
>> have a javascript (declared  as Media class inside a ModelAdmin class) that
>> reads user input in the textbox. But I am unable to this choice list back
>> from .js file to django admin. How do I do that? Thanks in advance.
>>
>> I have tried this.
>>
>> *models.py*
>> class Routing(models.Model):
>>     choice_text = models.CharField(_('Choices'), max_length=100,
>> default="(any)", null=False)
>>
>> *admin.py*
>> class AdminRoutingInlineForm(forms.ModelForm):
>>     def __init__(self, *args, **kwargs):
>>         super(AdminRoutingInlineForm, self).__init__(*args, **kwargs)
>>         CHOICES = [('a', 'any'),('b', 'blah'),]     * // override this
>> with choices from populate_dropdown.js (code below)*
>>         self.fields['choice_text'].choices = CHOICES
>>
>> class RoutingInlineAdmin(StackedInline):
>>     form = AdminRoutingInlineForm
>>     fields = (('choice_text', 'next'),)
>>     model = Routing
>>
>>
>> class FormModelAdmin(ModelAdmin):
>>     inlines = [RoutingInlineAdmin]
>>
>>     class Media:
>>         js= ("sforms/admin/populate_dropdown.js",)
>>
>>
>> *populate_dropdown.js*
>> (function($) {
>>     $(document).ready(function() {
>>
>>         $("[id^=id_fields-][id$=_options_0]").each(function(){     *  //
>> user input from this field will be used as choices*
>>         choices = '(any),' + $(this).val().split("\n");
>>         alert(choices);
>>
>>         *// send this choices back to admin.py and override CHOICES
>> in AdminRoutingInlineForm class*
>>         });
>> });
>>
>>
>>
>> --
> 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 [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/6ef56f22-dce0-4a85-b348-381b3a93e88f%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/6ef56f22-dce0-4a85-b348-381b3a93e88f%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CALXYUbkkYGyXuFWSgO6Q6nQuh0xsUH6GYJFhvcdmTmZpegXC-A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to