{ "info": { "author": "Rune Kaagaard", "author_email": "rumi.kg@gmail.com", "bugtrack_url": null, "classifiers": [], "description": "## About Django Admin FlexSelect ##\n\nFlexSelect is a small app for the Django Admin that makes it trivial to have\nforeign keys depend on each other. By depend I mean that choices and additional \ncontent of one field updates dynamically when another is changed.\n\n## Usage example ##\n\nSee the video at http://www.youtube.com/watch?v=ooii3iCTZ6o.\n\nIn the following we will define a Case model with two foreign key fields, a \nbase field `client` and a trigger field `company_contact_person`. When we \nchange the client on the Case change view the company_contact_person updates \naccordingly. Furthermore we will display the customer_contact_persons company \nand email as additional details. \n\nIn \"models.py\":\n\n```python\nfrom django.db import models as m\nfrom django.core.exceptions import ValidationError\n\n\"\"\"\nNo changes to the models are needed to use flexselect.\n\"\"\"\n\nclass Company(m.Model):\n name = m.CharField(max_length=80)\n \n def __unicode__(self):\n return self.name\n\nclass CompanyContactPerson(m.Model):\n company = m.ForeignKey(Company)\n name = m.CharField(max_length=80)\n email = m.EmailField()\n \n def __unicode__(self):\n return self.name\n \nclass Client(m.Model):\n company = m.ForeignKey(Company)\n name = m.CharField(max_length=80)\n \n def __unicode__(self):\n return self.name\n \nclass Case(m.Model):\n client = m.ForeignKey(Client)\n company_contact_person = m.ForeignKey(CompanyContactPerson)\n \n def clean(self):\n \"\"\"\n Makes sure the company for client is the same as the company for the \n customer contact person. Note that you need to check for `None` too\n if the fields are not required.\n \"\"\"\n if not self.client.company == self.company_contact_person.company:\n raise ValidationError('The clients and the contacts company does'\n ' not match.')\n \n def __unicode__(self):\n return u'Case: %d' % self.id\n```\n\nIn \"admin.py\":\n\n```python\nfrom django.contrib import admin\nfrom flexselect import FlexSelectWidget\nfrom test_flexselect.tests.models import (Company, Case, Client, \n CompanyContactPerson,)\n\nclass CompanyContactPersonWidget(FlexSelectWidget):\n \"\"\"\n The widget must extend FlexSelectWidget and implement trigger_fields, \n details(), queryset() and empty_choices_text().\n \"\"\"\n \n trigger_fields = ['client']\n \"\"\"Fields which on change will update the base field.\"\"\"\n \n def details(self, base_field_instance, instance):\n \"\"\"\n HTML appended to the base_field.\n \n - base_field_instance: An instance of the base_field.\n - instance: A partial instance of the parent model loaded from the\n request.\n \n Returns a unicoded string.\n \"\"\"\n return u\"\"\"\\\n