Friday, October 30, 2015

Re: return values from static files to django admin

bump!!

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+unsubscribe@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/ea61e786-51c3-4d98-86fc-b49695cfcb03%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment