Hi,

You may need to also calculate the possible options in python. Some hacky 
code that might help:

class AdminRoutingInlineForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(AdminRoutingInlineForm, self).__init__(*args, **kwargs)
        field1 = self.data.get('field1', '')
        CHOICES = [('a', '%s any' % field1),('b', '%s blah' % field1),] 
        self.fields['choice_text'].choices = CHOICES

Collin

On Monday, November 2, 2015 at 9:32:58 PM UTC+1, dc wrote:
>
> I tried that. But in that case I need to declare choices in models.py for 
> that field (so that the field is displayed as a dropdown in admin) and then 
> django doesn't accept any string that's not part of the choices list. I am 
> pretty sure I am missing something here.
>
> On Mon, Nov 2, 2015 at 2:55 PM, Andreas Kuhne <andrea...@suitopia.com 
> <javascript:>> wrote:
>
>> Hi,
>>
>> Yes you could just populate the dropdown list with javascript. 
>>
>> Regards,
>>
>> Andréas
>>
>> 2015-11-02 17:27 GMT+01:00 dc <ana...@vt.edu <javascript:>>:
>>
>>> Thanks a lot. Let me look into it.
>>>
>>> Is there any other way I can populate my choice list with user input?
>>>
>>> On Monday, November 2, 2015 at 10:19:22 AM UTC-5, Andréas Kühne wrote:
>>>>
>>>> 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 <ana...@vt.edu>:
>>>>
>>>>> 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 django-users...@googlegroups.com.
>>>>> To post to this group, send email to django...@googlegroups.com.
>>>>> 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 django-users...@googlegroups.com <javascript:>.
>>> To post to this group, send email to django...@googlegroups.com 
>>> <javascript:>.
>>> 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/24ac486e-08fa-42cd-a426-bda97246693c%40googlegroups.com
>>>  
>>> <https://groups.google.com/d/msgid/django-users/24ac486e-08fa-42cd-a426-bda97246693c%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 a topic in the 
>> Google Groups "Django users" group.
>> To unsubscribe from this topic, visit 
>> https://groups.google.com/d/topic/django-users/StPODUX2umA/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to 
>> django-users...@googlegroups.com <javascript:>.
>> To post to this group, send email to django...@googlegroups.com 
>> <javascript:>.
>> 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/CALXYUbmHYNcziqauv07arQPtfMfKrvBiBwkRiwYJW7apCopvaQ%40mail.gmail.com
>>  
>> <https://groups.google.com/d/msgid/django-users/CALXYUbmHYNcziqauv07arQPtfMfKrvBiBwkRiwYJW7apCopvaQ%40mail.gmail.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 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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/3b8221ae-751a-4e61-abaf-168fd7ff1688%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to