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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:02:45+00:00 2026-05-14T20:02:45+00:00

Using Django 1.1.1 In models.py: class Site(models.Model): name = models.CharField(max_length=50) class SiteMonth(models.Model): site =

  • 0

Using Django 1.1.1

In models.py:

class Site(models.Model):
    name = models.CharField(max_length=50)

class SiteMonth(models.Model):
    site = models.ForeignKey(Site)
    timestamp = models.DateTimeField()
    parameter1 = models.IntegerField()
    ....

In site_view.py:

#form for selecting which site to analyse
class SiteForm(forms.Form):
    site = forms.ModelChoiceField(queryset=Site.objects.all())

#form for selecting which site month to analyse
class SiteMonthForm(forms.Form):
    month = forms.ModelChoiceField(queryset=SiteMonth.objects.all())

def site(request,site=None):
    siteForm = SiteForm(request.GET)
    if not site==None:
        siteMonthForm = SiteMonthForm(request.GET)
        .....

And in urls.py:

(r'^site/$', 'my_project.site_view.site'),
(r'^site/(?P<site>\d+)/', 'my_project.site_view.site'),

So what I’m doing is making a page which can be used to look at the details of a particular Site. If no site id is given in the url, a form exists to select one, which will then redirect to the right url (via javascript in the template).

What I want to do, once a Site is selected, is to have a form in the page permitting selection of the SiteMonth objects belonging to that site for further investigation. My code above currently lists all SiteMonth objects for all sites. How do I set the SiteMonth form to only populate with values relating to the site id given in the GET request?

I can think of a couple of ways to do this, neither particularly elegant – what’s the proper django way?

  • 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-14T20:02:46+00:00Added an answer on May 14, 2026 at 8:02 pm

    Change the SiteMonthForm to something like this:

    class SiteMonthForm(forms.Form):
        month = forms.ModelChoiceField(queryset=SiteMonth.objects.all())
        def __init__(self, site, *args, **kwargs):
            super(SiteMonthForm, self).__init__(*args, **kwargs)
            self.fields["month"].queryset = SiteMonth.objects.filter(site__id=site)
    

    And use it like this:

    def site(request,site=None):
        siteForm = SiteForm(request.GET)
        if not site==None:
            siteMonthForm = SiteMonthForm(site, request.GET)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following models (cut down for understanding): class Venue(models.Model): name = models.CharField(unique=True) class
I'm trying to find the actual class of a django-model object, when using model-inheritance.
Using Django's built in models, how would one create a triple-join between three models.
I'm rewriting a PHP+MySQL site that averages 40-50 hits a day using Django. Is
I am building an article site using django. I've added many to many relation
I'm considering using Django for a project I'm starting (fyi, a browser-based game) and
I'm using Django to write a blog app, and I'm trying to implement a
I am having problems using django-tagging . I try to follow the documentation but
I'm using Django and Python 2.6, and I want to grow my application using
I'm using django and when users go to www.website.com/ I want to point them

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.