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

  • SEARCH
  • Home
  • 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 8401097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:43:07+00:00 2026-06-09T21:43:07+00:00

Taking a classic foo bar example: In models.py : Class Foo(models.Model): name = models.CharField(max_length=

  • 0

Taking a classic foo bar example:

In models.py:

Class Foo(models.Model):
    name = models.CharField(max_length= 200)

Class Bar(models.Model):
    name = models.CharField(max_length= 200)
    foo = models.ForeignKey('Foo')

In my form, I tried to limit the choices of my foreignkey to Foo to a subset of Foo using a raw query.

In forms.py:

class BarForm(ModelForm):
    search_field = CharField(max_length=100, required=False)

    def __init__(self,*args,**kwargs):
        search_str = kwargs.pop('search_str', None)
        super(BarForm,self ).__init__(*args,**kwargs)
        self.fields['search_field'].initial = search_str
        self.fields['foo'].queryset = Bar.objects.raw("""
select f.id as id, f.name as name from bar_lookup(%s)""", [search_str])

    class Meta:
        model = Bar
        exclude = ('foo',)

bar_lookup(%s) is a DB procedure returning a table. It searches multiple relations and handles the filtering and sorting of the results in an optimized way. It works and I would rather not have to code it again in Django code.

I get the following error: "'RawQuerySet' object has no attribute 'all'".
The form works if I use a normal Bar.objects.filter() instead.

How should I transform my RawQuerySet into a normal QuerySet?
Should I use the self.fields['line_stop'].choice option?

  • 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-09T21:43:08+00:00Added an answer on June 9, 2026 at 9:43 pm

    I had a similar problem and come with that tricky solution:

    class BarForm(ModelForm):
        search_field = CharField(max_length=100, required=False)
    
        def __init__(self,*args,**kwargs):
            search_str = kwargs.pop('search_str', None)
            super(BarForm,self ).__init__(*args,**kwargs)
            self.fields['search_field'].initial = search_str
            self.foo_choices = Bar.objects.raw("""
                  select f.id as id, f.name as name from bar_lookup(%s)""", [search_str])
            self.fields['foo'].choices = [(x.id, x) for x in self.foo_choices]
    
        def clean_foo(self):
            foo = self.cleaned_data['foo']
            if foo not in self.foo_choices:
                 raise forms.ValidationError("Some error!")
            return foo
    
        class Meta:
            model = Bar
    

    An I know it is not perfect, and subclassing ModelChoiceField would be better.

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

Sidebar

Related Questions

Taking Fabien Potencier's example : class User { function __construct($storage) { $this->storage = $storage;
Taking this file as an example, I'm trying to read the data in a
Taking the jQuery framework for example, if you run code like this: $(document).ready(function init()
Taking Ryan Bates' asciicast as an example: http://asciicasts.com/episodes/163-self-referential-association He ends with two associations of
Taking an example that is provided on the Fluent nHibernate website, I need to
Taking the GNU make application as an example, when I create a makefile it
Suppose that I have the following HBM mapping: <class name=Student table=student> <set name=classes table=student_classes
Taking this basic example of jQuery Validate (see link below), how can you instruct
Taking the following string example, what is the pattern I should use to extract
Taking this thread to the next level and now making a Main.as class to

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.