return values from static files to django admin

45 views
Skip to first unread message

dc

unread,
Oct 29, 2015, 11:40:32 PM10/29/15
to Django users
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
        });  
});
 


Message has been deleted

dc

unread,
Nov 2, 2015, 9:57:20 AM11/2/15
to Django users
Any lead will be extremely helpful. I am still stuck. :(

Andreas Kuhne

unread,
Nov 2, 2015, 10:19:22 AM11/2/15
to django...@googlegroups.com
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

--
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.
For more options, visit https://groups.google.com/d/optout.

dc

unread,
Nov 2, 2015, 11:27:57 AM11/2/15
to Django users
Thanks a lot. Let me look into it.

Is there any other way I can populate my choice list with user input?

Andreas Kuhne

unread,
Nov 2, 2015, 2:57:08 PM11/2/15
to django...@googlegroups.com
Hi,

Yes you could just populate the dropdown list with javascript. 

Regards,

Andréas

ananya choudhury

unread,
Nov 2, 2015, 3:32:58 PM11/2/15
to Django users
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.

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.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

Collin Anderson

unread,
Nov 5, 2015, 11:17:11 AM11/5/15
to Django users
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
Reply all
Reply to author
Forward
0 new messages