Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8973465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:27:11+00:00 2026-06-15T18:27:11+00:00

So let’s say I have jQuery convert a simple text input from this <input

  • 0

So let’s say I have jQuery convert a simple text input from this

<input class='employee_list' name='requestor' type='text' />

to this

<div name='requestor' class='ajax_picker'>
    <input class='search_box' class='employee_list' name='requestor_text' type='text'/>
    <input class='id' name='requestor' type='hidden' value='' /> 
    <div class='results'></div>
</div>

And load the hidden input value with AJAX + JSON. Problem is, if form.is_valid() is not True, then how can I create a custom Widget that will render both values? Thinking I could do two different fields, but that seems ugly. I could also customize the form rendering, but that’s even worse. Maybe the form can pass all of the POST data to the widget, but can’t seem to figure out how to get that to work.

There must be an elegant way to achieve this!

from django import forms

class AjaxPickerWidget(forms.TextInput):
    def render(self, name, value, attrs=None):
        # ... now what?
        return super(AjaxPickerWidget, self).render(name, value, attrs=attrs)

My Solution

Thanks for your help, I took a variant approach. Since I am using this strictly for Models (hence needing a key/value pair) I made the widget interact directly with the model and create a data-id attribute that the jQuery would catch to move to the hidden field.

from django import forms

class AjaxPickerModelWidget(forms.TextInput):
    def __init__(self, css_class, queryset, id_name, value_name, attrs={}):
        attrs['class'] = css_class
        self.queryset = queryset
        self.id_name = id_name
        self.value_name = value_name
        super(AjaxPickerModelWidget, self).__init__(attrs=attrs)

    def render(self, name, value, attrs={}):
        try:
            instance = self.queryset.get(**{self.id_name: value})
            attrs['data-id'] = value
            value = getattr(instance, self.value_name)
        except:
            value = ''
        return super(AjaxPickerModelWidget, self).render(name, value,
                                                         attrs=attrs)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:27:13+00:00Added an answer on June 15, 2026 at 6:27 pm

    Well, I don’t know if this qualifies as elegant, but how about something like this. Here I’m storing the search string as a property on the widget, and setting the property from the POST data in the form constructor. I’m also using name=”foo_picker_text” to indicate the search input for the corresponding input named “foo”.

    class AjaxPickerWidget(HiddenInput):
      search_text_suffix = "_picker_text"
    
      def __init__(self, **kwargs):
        super(AjaxPickerWidget, self).__init__(**kwargs)
        self.search_text = None
    
      def render(self, name, value, attrs=None):
        start = '<div name="%s" class="ajax_picker">' % (name) + \
                 '<input class="search_box" name="%s%s" ' % (name, self.search_text_suffix)+ \
                 'type="text" value="%s" />' % (self.search_text or "")
        end = super(AjaxPickerWidget, self).render(name, value, attrs=attrs) + '</div>'
        return '%s%s' % (start, end)
    
    class MyForm(Form):
      requestor = CharField(widget = AjaxPickerWidget())
    
      def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)
        # find picker text in POST data and tell the corresponding widget about it
        for name in self.data:
          if name.endswith(AjaxPickerWidget.search_text_suffix):
            field_name = name.split(AjaxPickerWidget.search_text_suffix)[0]
            self.fields[field_name].widget.search_text = self.data[name]
    

    You might have to tweak it a bit to get attributes to show up where you want them, etc. but hopefully this basically does what you want?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have some text as follows: do this, do that, then this,
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I have a text file composed like this ##### typeofthread1 ##### typeofthread2
Let's say that I have classes like this: public class Parent { public int
Let's say I have a simple UserControl with no codebehind: <UserControl xmlns= .... x:Class=TrafficLight.LightControl>
Let say you have an XML like this: <?xml version=1.0 encoding=utf-8?> <Class HashCode=307960707> <Person>
Let me explain best with an example. Say you have node class that can
Let's say I have a sortable list like this: $(.song-list).sortable({ handle : '.pos_handle', axis
Let's say I have a string like this: var str = /abcd/efgh/ijkl/xxx-1/xxx-2; How do
Let's say I have two text files that I need to extract data out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.