Hi, I am new to Django, I have read a good bit of the documentation and am trying to write an app. This app is simple and should allow for a lost of contacts and companies. Each company has different locations (or addresses) such as main office, branch office etc. Each contact should be assigned to only one company and one location. So that when looking up a contact the Name, Company, and Address can all be correct.
In the Django admin. I have company and location set-up with an Stacked inline so that the company name and all locations can be entered at once. Then I have a separate admin page for the contact. What I want to do is filter the drop-down list for the contact so that once a company is selected only the locations related to that company are displayed. I found this on filtering drop downs in admin http://www.stereoplex.com/blog/filtering-dropdown-lists-in-the-django-admin but I still cannot get it to work. The admin site works, it just lists all the Locations in the database weather a company is selected or not. Here are my models: from django.db import models class Company(models.Model): company_name = models.CharField(max_length=200) website = models.URLField(verify_exists=False, max_length=200, blank=True) company_type = models.CharField(max_length=200, blank=True) company_trade = models.CharField(max_length=200, blank=True) company_notes = models.TextField(blank=True) custom = models.TextField(blank=True) def __unicode__(self): return self.company_name def is_subcontractor(self): return self.company_type == "subcontractor" class Location(models.Model): description = models.CharField(max_length=200) company = models.ForeignKey(Company, related_name='location') address1 = models.CharField(max_length=200) address2 = models.CharField(max_length=200, blank=True) address3 = models.CharField(max_length=200, blank=True) city = models.CharField(max_length=200) state = models.CharField(max_length=2) zip_code = models.CharField(max_length=10) main_tel = models.CharField(max_length=200, blank=True) location_notes = models.TextField(blank=True) custom = models.TextField(blank=True) def __unicode__(self): return self.description class Contact(models.Model): first_name = models.CharField(max_length=200) last_name = models.CharField(max_length=200) email = models.EmailField(max_length=75) company = models.ForeignKey(Company) location = models.ForeignKey(Location) title = models.CharField(max_length=200, blank=True) direct_tel = models.CharField(max_length=200, blank=True) mobile_tel = models.CharField(max_length=200, blank=True) contact_notes = models.TextField(blank=True) custom = models.TextField(blank=True) def __unicode__(self): return self.first_name + ' ' + self.last_name Here is admin.py: from contacts.models import Company,Location,Contact from django.contrib import admin from django.forms.models import BaseModelFormSet class LocationInLine(admin.StackedInline): model = Location extra = 1 fieldsets = [ (None, {'fields': ['description','address1','address2','address3','city','state','zip_code', 'main_tel']}), ('Additional Notes', {'fields': ['location_notes', 'custom'], 'classes': ['collapse']}) ] class ContactLocationFormset(BaseModelFormSet): def add_fields(self, form, index): super(ContactLocationFormset, self).add_fields(form, index) locations = Location.objects.none() if form.instance: try: company = form.instance.Company except Company.DoesNotExist: pass else: locations = Locations.objects.filter(company=Location.company) form.fields['location'].queryset = locations class ContactAdmin(admin.ModelAdmin): model = Contact formset = ContactLocationFormset fieldsets = [ (None, {'fields': ['first_name','last_name','email','company','location','title','direct_tel','mobile_tel']}), ('Additional Notes', {'fields': ['contact_notes', 'custom'], 'classes': ['collapse']}) ] class CompanyAdmin(admin.ModelAdmin): fieldsets = [ (None, {'fields': ['company_name','website','company_type','company_trade']}), ('Additional Notes', {'fields': ['company_notes', 'custom'], 'classes': ['collapse']}) ] inlines = [LocationInLine] admin.site.register(Company,CompanyAdmin) admin.site.register(Contact,ContactAdmin) -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. For more options, visit https://groups.google.com/groups/opt_out.