OceanTara Code Blog

February 21, 2009

use InlineModelChoiceField for hidden foreign key relationships in django

Filed under: django, python — chris @ 10:04 pm

django offers a ModelChoiceField to map foreign keys. The problem is if you have a foreign key you don’t want the user to be able to change and hence have the widget=HiddenInput. As is you still have to provide a queryset=MyModel.objects.all(). This is obviously bad for performance, security, and clarity of code. Here’s a terrific, easy solution.

http://www.davidcramer.net/code/109/modelchoicefields-as-charfields-in-django.html

Powered by WordPress