OK, I've been tinkering with this all day, and I'm still having one
big problem

As Jeremy suggested, I created custom ZipCodeField and MyAdmin
classes. In addition, I created a custom ZipcodeLookupInput widget.

I've tested this in the shell and on the test server, and the clean
and render functions appear to be working beautifully. The widget's
render() function looks up the initial value (the zip code id) and
uses the corresponding zip code as its display value, and the field's
clean() function converts the user-provided zip into an id and returns
it as an integer value.

Unfortunately, I keep getting an error when I try to save. It appears
that the zip_code_id is None by the time a DB query is attempted in
django.db.backends.util. But this makes no sense, beacuse the clean()
function seems to be working properly (i.e., it returns an integer
value).

Does anyone know what might be happening?  I have pasted my custom
classes code below.


from mysite.address.models import Postal_Code
from django import newforms as forms

[...]

class ProfileOptions(admin.ModelAdmin):
        def formfield_for_dbfield(self, db_field, **kwargs):
                if db_field.name == 'zip_code':
                        return ZipCodeField(**kwargs)
                else:
                        return 
super(ProfileOptions,self).formfield_for_dbfield(db_field,
**kwargs)

class ZipCodeField(forms.fields.CharField):
        def __init__(self, **kwargs):
                kwargs['widget'] = ZCLookupTextInput
                self.max_length, self.min_length = 5, 1
                super(ZipCodeField, self).__init__(**kwargs)

        def clean(self, value):
                super(ZipCodeField, self).clean(value)
                try:
                        zip_code_id = 
Postal_Code.objects.get(postal_code=value).id
                except Postal_Code.DoesNotExist:
                        raise ValidationError, "Please enter a valid zip code."
                return int(zip_code_id)


class ZCLookupTextInput(forms.widgets.TextInput):
    def render(self, name, value, attrs=None):
                try:
                        zip_code = Postal_Code.objects.get(id = 
value).postal_code
                except:
                        zip_code = value
                return super(LookupTextInput, self).render(name, zip_code, 
attrs)




--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to