Hello, I tried to send this several months ago via the Gmane NNTP server, but I suspect it didn't get through. At least I can't find it.
The same problems seems to exist with the current subversion code. My apologies if this did get through and was discussed, if so could somebody please provide a link to the discussion? Thanks. ----- Forwarded message from Brian May <br...@vpac.org> ----- Date: Fri, 13 Mar 2009 12:26:12 +1100 From: Brian May <br...@vpac.org> Subject: selecting foreign keys To: undisclosed-recipients: ; Newsgroups: gmane.comp.python.django.user Hello, A frequent problem people seem to have with Django[1] is the default interface for selecting foreign keys is slow and clumsy to use if there are lots of selections. To the best of my knowledge there is no solution included with Django. So I have created my own field type that is derived from the django.forms.CharField form type. The Clean method will look up the value in the db with the matching name, and return the result. Simple. Efficient. No need to repeatedly query the database/objects for the same data, if the same foreign key appears multiple times on the same form. I like it. The problem comes to using this with the admin interface. I created a class derived from django.db.models.ForeignKey that overrides the formfield method to use my field type. Unfortunately this does not work[2]. The issue is that the admin interface automatically wraps the widget with the RelatedFieldWidgetWrapper, which does not work because this wrapper assumes the widget is compatible with the Select field. However, the widget used by CharField is not compatible, as it (obviously) doesn't have a concept of choices. So I get the error: 'TextInput' object has no attribute 'choices' From: /home/brian/tree/django/django/contrib/admin/widgets.py in ___init__, line 203 Is there anyway I can tell it not to wrap the widget in this manner? The wrapping seems to take place at ~/tree/django/django/contrib/admin/options.py, line 140, inside the formfield_for_dbfield function - it looks like it checks the field type, but makes no attempt to check the widget type is compatible. Brian May [1] Version information, reported by subversion Path: . URL: http://code.djangoproject.com/svn/django/branches/releases/1.0.X Repository Root: http://code.djangoproject.com/svn Repository UUID: bcc190cf-cafb-0310-a4f2-bffc1f526a37 Revision: 10025 Node Kind: directory Schedule: normal Last Changed Author: ikelly Last Changed Rev: 10025 Last Changed Date: 2009-03-11 09:50:37 +1100 (Wed, 11 Mar 2009) [2] The code is still on the messy side - especially with regards to the parameter list to __init__ on Object_Name_Field class. It hasn't been tested properly yet either. However in case people want to see it... from django.db import models from django.forms.util import ValidationError from django import forms class Object_Name_Field(forms.CharField): def __init__(self, object, queryset=None, to_field_name=None, *args, **kwargs): self.object = object super(Object_Name_Field, self).__init__(*args, **kwargs) def clean(self, value): value=super(Object_Name_Field, self).clean(value) if value in ('',None): return None try: clean=self.object.objects.get(name=value) except self.object.DoesNotExist, e: raise ValidationError(u"Cannot find object %s: %s" % (value,e)) return clean class Foreign_Name_Key(models.ForeignKey): def formfield(self, **kwargs): # This is a fairly standard way to set up some defaults # while letting the caller override them. defaults = {'form_class': Object_Name_Field, 'object': self.rel} defaults.update(kwargs) return super(Foreign_Name_Key, self).formfield(**defaults) ----- End forwarded message ----- -- Brian May <br...@microcomaustralia.com.au> --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---