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

The Archive Base Latest Questions

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

I have two models milestone and project, the code as below: class Project(models.Model): ID

  • 0

I have two models “milestone” and “project”, the code as below:

class Project(models.Model):
    ID = models.CharField(max_length=15, primary_key=True)
    Name = models.CharField('Project Name', max_length=100)
    ShortName = models.CharField('Project Short Name', max_length=100)
    def __unicode__(self):
        return self.Name

class Milestone(models.Model):
Project = models.ForeignKey(Project)
def __unicode__(self):
    return self.Name

When i create a milestone, i can select a project and then the milestone belongs to that project, for example, project1---> m1; project2 --->m2
Then in an other model “Task”, i want to create a task belong to a special project and milestone. The code is as below:

class Task(models.Model):
    Name = models.CharField('Title', max_length=200)  
    Project = models.ForeignKey(Project,null=True, blank=True)
    Milestone = models.ForeignKey(Milestone,null=True, blank=True)
    def __unicode__(self):
        return self.Name

The problem is that when i create a Task and select a project, then the itemlist of the milestone field is always including two “m1” and “m2”, i want that when i select project1 the list only show m1, and when i select project2 the list only show m2.

How can i implement this? Thanks

Updated
My folder structure

 mysite_new/

      manage.py
      mysite/
      ------ __init__.py
             urls.py
             setting.py
             wsgi.py
             templates/
                      default.html

      ticket/
      ------__init__.py
            models.py
            view.py
            urls.py
            admin.py
            js/
              ---- project_change.js

in admin.py

class TaskAdmin(admin.ModelAdmin):
    class Media:
          js = ['js/project_change.js',]
admin.site.register(Task,TaskAdmin)

In urls.py

    urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^admin/login', MyView().login),

    url(r'^hello/', hello),
    (r'^product_change/','project_choices'),


)

And in my db
enter image description here
In the add task web site:http://127.0.0.1:8000/admin/ticket/task/add/
enter image description here

I don’t change any of your code, could you please help to check what is the wrong with my code ?

Update
Thanks very much. Arulmurugan, a kind-hearted man

  • 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:23:50+00:00Added an answer on June 15, 2026 at 6:23 pm

    You achieve this using jQuery and Ajax. Try using the following:

    project_change.js

    (function($){   
        $(function(){
            $(document).ready(function() {
                $('#id_Project').bind('change', project_change);            
                $('#id_Milestone > option').show();
                if ($('#id_Project').val() != '') {
                    var project_id = $('#id_Project').val();
                    $.ajax({
                    "type"      : "GET",
                  "url"         : "/product_change/?project_id="+project_id,
                    "dataType"  : "json",
                  "cache"       : false,
                    "success"   : function(json) {
                        $('#id_Milestone >option').remove();
                        for(var j = 0; j < json.length; j++){
                            $('#id_Milestone').append($('<option></option>').val(json[j][0]).html(json[j][1]));
                        }
                    }           
                });
                }
            });
        });  
    })(django.jQuery);
    
    // based on the project, milestone will be loaded
    
    var $ = django.jQuery.noConflict();
    
    function project_change()
    {
        var project_id = $('#id_Project').val();
        $.ajax({
        "type"      : "GET",
      "url"         : "/product_change/?project_id="+project_id,
        "dataType"  : "json",
      "cache"       : false,
        "success"   : function(json) {
            $('#id_Milestone > option').remove();
            for(var j = 0; j < json.length; j++){
                $('#id_Milestone').append($('<option></option>').val(json[j][0]).html(json[j][1]));
            }
        }           
    })(jQuery);
    }
    

    Include the following in views.py:

    from django.shortcuts import HttpResponse
    from django.utils import simplejson
    
    from ticket.models import Milestone
    
    def project_choices(request): 
        milestone_list = []
        project_id = request.GET.get('project_id')
        milestones = Milestone.objects.filter(project = project_id)    
        [milestone_list.append((each_milestone.pk,each_milestone.name)) for each_milestone in milestones]
        json = simplejson.dumps(milestone_list)
        return HttpResponse(json, mimetype='application/javascript')
    

    In urls.py:

    from ticket.views import project_choices
    
    urlpatterns = patterns(
        (r'^product_change/', project_choices),
    )
    

    In admin.py where you want to load milestone based on project:

    class Media:
        js = ['/path/to/project_change.js',]
    

    I hope this will help you.

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

Sidebar

Related Questions

I have two models: class Studio(models.Model): name = models.CharField(Studio, max_length=30, unique=True) class Film(models.Model): studio
I have two models: class Contact(models.Model): name = models.CharField(max_length=255) class Campaign(models.Model): contact = models.ForeignKey(Contact,
I have two models such that class JobTitle(models.Model): name = models.CharField(max_length=1000) class Employer(models.Model): jobtitle
I have two models like this: class Store(models.Model): name = models.CharField(max_length=255) class Order(models.Model): store
I have two models like this: class ClassA(models.Model): ida = models.AutoField(primary_key=True) classb = models.ForeignKey(ClassB)
I have two models. Comment and his Subcomments: class Comment(models.Model): .... author = models.CharField(max_length=80)
I have two models for store and city: class City(models.Model): name = models.CharField() slug
I have two models in Django like follows(in pseudo code) class Medicine(db.Model): field_1 =
I have two models class Market(Model): title=models.Charfield() class Product(Model): title=Models.Charfield() markets=ManyToMany(Market) And i want
I have two models in separate apps: # Groups app class Group(models.Model): name =

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.