Login

Tag "foreign-key"

Snippet List

Allow foreign key attributes in list_display with '__'

This snippet provides a subclass of admin.ModelAdmin that lets you span foreign key relationships in list_display using '__'. The foreign key columns are sortable and have pretty names, and select_related() is set appropriately so you don't need queries for each line. EDITS: * Fixed error when DEBUG=False. * Broke out `getter_for_related_field` so you can override short_description manually (see example). * Added regular foreign key fields to select_related(), since this is overriding the code in ChangeList that usually does it.

  • admin
  • foreign-key
  • list_display
Read More

Foreign Key list_filter wthout custom FilterSpec

This is some (probably) pretty dodgy code that allows for foreign keys in the admin's list_filter without using a custom FilterSpec. It overrides the django.db.models.options.Options get_field class to handle spanned relationships, i.e. list_filter=('house__room__town',) as seen in http://code.djangoproject.com/ticket/3400 I have only tested this with a double foreign key relationship('house__room'), and it worked. It hasn't been tested in production, and the troubling part of this code is probably to use of 'deepcopy'. I do add those copies back to the options instance, so there shouldn't be too much runaway copying.

  • foreign-key
  • filterspec
  • list-filter
  • 3400
Read More

PostgreSQL ON DELETE CASCADE

Have you always been annoyed by how you set up this elaborate big database schema and weren't able to have **ON DELETE CASCADE ON UPDATE CASCADE** in dbshell? This solves the problem; create the two files and and empty *__init__.py* and put them somewhere in your path. Then say DATABASE_ENGINE='postgresql_psycopg2_cascade' in settings. Really I'd like this to be in the ForeignKey object, were it upstream Django or an own version of it, but it doesn't seem possible. Ideas on how to make this configurable are more than welcome! Props go out to Ari Flinkman for the inspiration to do this!

  • database
  • postgres
  • foreign-key
  • postgresql
  • databases
Read More
Author: mjt
  • 1
  • 1

URL models

You can use `UrlModel` to provide URL functionality to any instance of any model and any language (language support can be removed from this). Each model must have own view method, that returns HttpResponse. I was inspired by Flatpages. It is useful for small sites and static pages. `class Page(UrlModel): text = models.TextField() def view(self, request) # do something here return HttpResponse(...)`

  • middleware
  • urls
  • models
  • foreignkey
  • model
  • generic
  • url
  • foreign-key
  • genericforeignkey
  • contenttypes
  • 404
  • contenttype
  • content-type
Read More

dumpdata/loaddata with MySQL and ForeignKeys

InnoDB tables within MySQL have no ability to defer reference checking until after a transaction is complete. This prevents most dumpdata/loaddata cycles unless the dump order falls so that referenced models are dumped before models that depend on them. This code uses [Ofer Faigon's](http://www.bitformation.com) topological sort to sort the models so that any models with a ForeignKey relationship are dumped after the models they reference. class Entry(models.Model): txt = .... class Comment(models.Model): entry = models.ForeignKey(Entry) This code will ensure that Entry always gets dumped before Comment. Fixtures are an important part of the django Unit Testing framework so I really needed to be able to test my more complicated models. **Caveats** 1. You use this snippet to dump the data and the built in manage.py loaddata to load the fixture output by this program. A similar solution could be applied to the XML processing on the loaddata side but this sufficed for my situations. 2. This code does not handle Circular or self-references. The loaddata for those needs to be much smarter.

  • mysql
  • loaddata
  • foreign-key
  • fixture
  • dumpdata
Read More

autocompleter with database query

This is an improvement of snippet 253 in that it supports database queries. Implementing autocompletion for foreign keys takes a few steps: 1) Put the snippet above into <app>/widgets/autocomplete.py. 2) Create a view of your foreign key model (here: Donator) in <app>/donator/views.py: from models import Donator from widgets.autocomplete import autocomplete_response def autocomplete(request): return autocomplete_response( request.REQUEST['text'], Donator, ( 'line_1', 'line_2', 'line_3', 'line_4', 'line_5', 'line_6', 'line_7', 'line_8', '^zip_code', 'location' ) ) This view returns the autocompletion result by searching the fields in the tuple. Each word from the form field must appear at least in one database field. 3) Create a URLconf that points to this new view. 4) In the form where you need the autocompletion, define the widget of the foreign key field as an instance of AutoCompleteField: from widget.autocomplete import AutoCompleteField field.widget = AutoCompleteField( url='/donator/autocomplete/'), options={'minChars': 3} ) The url parameter is the URL connected to the view in step 3), the options dict is passed on to the Ajax.Autocompleter JavaScript object. Links: * [Snippet 253](http://www.djangosnippets.org/snippets/253/) * [Django and scriptaculous integration](http://wiki.script.aculo.us/scriptaculous/show/IntegrationWithDjango) * [Ajax.Autocompleter](http://wiki.script.aculo.us/scriptaculous/show/Ajax.Autocompleter)

  • ajax
  • selection
  • database
  • autocomplete
  • query
  • foreign-key
  • prototype
  • scriptaculous
  • protoculous
Read More

6 snippets posted so far.