{ "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
\n
\n
%s
%s
\n
%s
%s
\n
\n
\n \"\"\" % ('Company', base_field_instance.company,\n 'Email', base_field_instance.email,\n )\n \n def queryset(self, instance):\n \"\"\"\n Returns the QuerySet populating the base field. If either of the\n trigger_fields is None, this function will not be called.\n \n - instance: A partial instance of the parent model loaded from the\n request.\n \"\"\"\n company = instance.client.company\n return CompanyContactPerson.objects.filter(company=company)\n \n def empty_choices_text(self, instance):\n \"\"\"\n If either of the trigger_fields is None this function will be called\n to get the text for the empty choice in the select box of the base\n field.\n \n - instance: A partial instance of the parent model loaded from the\n request.\n \"\"\"\n return \"Please update the client field\"\n \nclass CaseAdmin(admin.ModelAdmin):\n def formfield_for_foreignkey(self, db_field, request, **kwargs):\n \"\"\"\n Alters the widget displayed for the base field.\n \"\"\"\n if db_field.name == \"company_contact_person\":\n kwargs['widget'] = CompanyContactPersonWidget(\n base_field=db_field,\n modeladmin=self,\n request=request,\n )\n kwargs['label'] = 'Contact'\n return super(CaseAdmin, self).formfield_for_foreignkey(db_field, \n request, **kwargs)\n\nclass ClientAdmin(admin.ModelAdmin):\n pass\n\nclass CompanyContactPersonAdmin(admin.ModelAdmin):\n pass\n\nclass CompanyAdmin(admin.ModelAdmin):\n pass\n\nadmin.site.register(Case, CaseAdmin)\nadmin.site.register(Client, ClientAdmin)\nadmin.site.register(CompanyContactPerson, CompanyContactPersonAdmin)\nadmin.site.register(Company, CompanyAdmin)\n```\n\n## Installation ##\n\n sudo pip install django-admin-flexselect\n \n## Configuration ##\n\n1. Add `\"flexselect\",` to `INSTALLED_APPS` in \"settings.py\".\n\n2. Add `(r'^flexselect/', include('flexselect.urls')),` to \"urls.py\".\n\n### Options ###\nAs of yet, flexselect only have one configuration option, namely \n\"include_jquery\" that if set to true will include jQuery and jQueryUI from \nthe google ajax CDN. It defaults to false and the entire FLEXSELECT dict can\nbe omitted.\n\n```python\n# Flexselect settings.\nFLEXSELECT = {\n 'include_jquery': True,\n}\n```\n\n### Static files ###\nFlexSelect requires \"django.contrib.staticfiles\" installed to work \nout of the box. If it is not then include \"jQuery\", \"jQueryUI\" and \n\"flexselect/static/flexselect/js/flexselect.js\" manually. Read more about \n\"django.contrib.staticfiles\" at \nhttps://docs.djangoproject.com/en/1.3/ref/contrib/staticfiles/.", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/runekaagaard/django-admin-flexselect", "keywords": null, "license": "Public Domain", "maintainer": null, "maintainer_email": null, "name": "django-admin-flexselect", "package_url": "https://pypi.org/project/django-admin-flexselect/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/django-admin-flexselect/", "project_urls": { "Download": "UNKNOWN", "Homepage": "https://github.com/runekaagaard/django-admin-flexselect" }, "release_url": "https://pypi.org/project/django-admin-flexselect/0.4.1/", "requires_dist": null, "requires_python": null, "summary": "Dynamic select fields for the Django Admin that just works.", "version": "0.4.1" }, "last_serial": 789047, "releases": { "0.4.1": [ { "comment_text": "", "digests": { "md5": "b1c6385cbd97cfb000fd990382240af5", "sha256": "cabf9cb7c8409e2b2cfbc0280a791109047339ff8d64af41287055065176d075" }, "downloads": -1, "filename": "django-admin-flexselect-0.4.1.tar.gz", "has_sig": false, "md5_digest": "b1c6385cbd97cfb000fd990382240af5", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6321, "upload_time": "2011-06-18T01:03:57", "url": "https://files.pythonhosted.org/packages/55/61/bf4a9851713525350e85b6fb96c48125ad92fb6cdd8adaf5bf80ae573911/django-admin-flexselect-0.4.1.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "b1c6385cbd97cfb000fd990382240af5", "sha256": "cabf9cb7c8409e2b2cfbc0280a791109047339ff8d64af41287055065176d075" }, "downloads": -1, "filename": "django-admin-flexselect-0.4.1.tar.gz", "has_sig": false, "md5_digest": "b1c6385cbd97cfb000fd990382240af5", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6321, "upload_time": "2011-06-18T01:03:57", "url": "https://files.pythonhosted.org/packages/55/61/bf4a9851713525350e85b6fb96c48125ad92fb6cdd8adaf5bf80ae573911/django-admin-flexselect-0.4.1.tar.gz" } ] }