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

In the add task web site:http://127.0.0.1:8000/admin/ticket/task/add/

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
You achieve this using jQuery and Ajax. Try using the following:
project_change.js
Include the following in views.py:
In urls.py:
In admin.py where you want to load milestone based on project:
I hope this will help you.