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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:47:51+00:00 2026-05-10T21:47:51+00:00

Say I have the following in my models.py : class Company(models.Model): name = …

  • 0

Say I have the following in my models.py:

class Company(models.Model):    name = ...  class Rate(models.Model):    company = models.ForeignKey(Company)    name = ...  class Client(models.Model):    name = ...    company = models.ForeignKey(Company)    base_rate = models.ForeignKey(Rate) 

I.e. there are multiple Companies, each having a range of Rates and Clients. Each Client should have a base Rate that is chosen from its parent Company's Rates, not another Company's Rates.

When creating a form for adding a Client, I would like to remove the Company choices (as that has already been selected via an "Add Client" button on the Company page) and limit the Rate choices to that Company as well.

How do I go about this in Django 1.0?

My current forms.py file is just boilerplate at the moment:

from models import * from django.forms import ModelForm  class ClientForm(ModelForm):     class Meta:         model = Client 

And the views.py is also basic:

from django.shortcuts import render_to_response, get_object_or_404 from models import * from forms import *  def addclient(request, company_id):     the_company = get_object_or_404(Company, id=company_id)      if request.POST:         form = ClientForm(request.POST)         if form.is_valid():             form.save()             return HttpResponseRedirect(the_company.get_clients_url())     else:         form = ClientForm()      return render_to_response('addclient.html', {'form': form, 'the_company':the_company}) 

In Django 0.96 I was able to hack this in by doing something like the following before rendering the template:

manipulator.fields[0].choices = [(r.id,r.name) for r in Rate.objects.filter(company_id=the_company.id)] 

ForeignKey.limit_choices_to seems promising but I don’t know how to pass in the_company.id and I’m not clear if that will work outside the Admin interface anyway.

Thanks. (This seems like a pretty basic request but if I should redesign something I’m open to suggestions.)

  • 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. 2026-05-10T21:47:51+00:00Added an answer on May 10, 2026 at 9:47 pm

    ForeignKey is represented by django.forms.ModelChoiceField, which is a ChoiceField whose choices are a model QuerySet. See the reference for ModelChoiceField.

    So, provide a QuerySet to the field’s queryset attribute. Depends on how your form is built. If you build an explicit form, you’ll have fields named directly.

    form.rate.queryset = Rate.objects.filter(company_id=the_company.id) 

    If you take the default ModelForm object, form.fields['rate'].queryset = ...

    This is done explicitly in the view. No hacking around.

    • 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 the following Django model: class Info(models.Model): instrument = models.ForeignKey('Instrument') date
Say I have the following model: class Foo(models.Model): content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField()
Let's say I have the following classes: class Votable(models.Model): name = ... class Vote(models.Model):
Lets say we have the following models: (simplified) class Collection(models.Model): slug = models.SlugField() class
Lets say we have following models. class User(db.Model): username=db.StringProperty() avatar=db.ReferenceProperty() class User(db.Model): username=db.StringProperty() avatar=db.StringProperty()
Ok say I have the following model: class Country < ActiveRecord::Base validates_presence_of :name validates_presence_of
Say I have the following models: class Image(models.Model): image = models.ImageField(max_length=200, upload_to=file_home) content_type =
Say I have the following models: class Baz(models.Model): winning = models.CharField(max_length=100) class Bar(models.Model): baz
Let's say I have the following Django models: class A(models.Model): keywords = models.ManyToManyField(Keyword) class
Let's say I have the following: class Employee(models.Model): firstName = models.CharField(max_length = 30) lastName

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.