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 581903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:37:46+00:00 2026-05-13T14:37:46+00:00

I’m using django-ajax-selects , which is a freely available django app providing jquery autocomplete

  • 0

I’m using django-ajax-selects, which is a freely available django app providing jquery autocomplete functionality.

I’ve got it working – i.e. it is autocompleting the form fields I want it to. But I have a problem… I’m using it in a ModelForm which adds Partnership objects to the database:

class Skater(models.Model):
    name = models.CharField(max_length=64)
    surname = models.CharField(max_length=64)
    gender = models.CharField(max_length=1, choices=GENDER_CHOICES)

class Partnership(models.Model):
    female_partner = models.ForeignKey(Skater, limit_choices_to = {'gender': FEMALE}, related_name='female_partner_set')
    male_partner = models.ForeignKey(Skater, limit_choices_to = {'gender': MALE}, related_name='male_partner_set')

I want the user to be able to put in a name and surname into the female_partner and male_partner field even if a Skater object like that doesn’t exist and I want that object created. How do I go about doing this? I cannot put the code in the form’s save method because the field won’t validate (it’s not a valid Skater).

EDIT 1:
Adding in more code…

The form:

class PartnershipAddForm(forms.ModelForm):
    female_partner = AutoCompleteSelectField('female_skater',required=True)
    male_partner = AutoCompleteSelectField('male_skater',required=True)

    class Meta:
        model = Partnership

settings.py:

AJAX_LOOKUP_CHANNELS = {
    'female_skater' : ('skaters.lookups', 'FemaleLookup'),
    'male_skater' : ('skaters.lookups', 'MaleLookup'),
}

lookups.py (MaleLookup is the same except that gender=MALE):

class FemaleLookup(object):

    def get_query(self,q,request):
        """ return a query set.  you also have access to request.user if needed """
        return Skater.objects.filter(Q(gender=FEMALE) & (Q(name__istartswith=q) | Q(surname__istartswith=q)))

    def format_item(self,skater):
        """ simple display of an object when it is displayed in the list of selected objects """
        return unicode(skater)

    def format_result(self,skater):
        """ a more verbose display, used in the search results display.  may contain html and multi-lines """
        return "%s<br/>" % unicode(skater)

    def get_objects(self,ids):
        """ given a list of ids, return the objects ordered as you would like them on the admin page.
            this is for displaying the currently selected items (in the case of a ManyToMany field)
        """
        return Skater.objects.filter(pk__in=ids).order_by('name','surname')
  • 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-05-13T14:37:47+00:00Added an answer on May 13, 2026 at 2:37 pm

    AutoCompleteSelectField holds the id of the object rather than the text, which is why I had the “required” error all along (and why Daniel’s solution doesn’t work). The value variable was empty as a Skater that doesn’t exist doesn’t have an id.

    I’m not sure this is the best way of doing this, but I ended up using AutoCompleteField instead of AutoCompleteSelectField. AutoCompleteField holds text, but it doesn’t create a Skater object for me.

    The code:

    class PartnershipAddForm(forms.ModelForm):
        female_partner = AutoCompleteField('female_skater',required=True)
        male_partner = AutoCompleteField('male_skater',required=True)
    
        class Meta:
            model = Partnership
    
        def save(self):
            partners = [self.cleaned_data['female_partner'],
                        self.cleaned_data['male_partner']]
            name = ['','']
            surname = ['','']
            for i in [0,1]:
                name[i],surname[i] = get_name_surname(partners[i])
            partners = [None,None]
            partners_created = [None,None]
            gender = [FEMALE,MALE]
            for i in [0,1]:        
                partners[i],partners_created[i] = Skater.objects.get_or_create(
                                                name=name[i],
                                                surname=surname[i],
                                                gender=gender[i]
                                            )
    
             partnership, created = Partnership.objects.get_or_create(
                                        female_partner=partners[0],
                                        male_partner=partners[1],
                                    )
             return partnership
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
We're building an app, our first using Rails 3, and we're having to build
I am using Paperclip to handle profile photo uploads in my app. They upload
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.