A Bit of Django Admin Trickery

OK, so I’ve been playing with the latest release of Django (1.1, well technically I’m using the latest SVN release) and am really enjoying the things they’ve added since 1.0.One major irritant I spent most of the morning working through, however, was something I think needs to be attended to, or at least clarified in the documentation.  I’ve got a model “Client” that can have more than one address “Address”, defined as a ForeignKey relationship.  I want to allow editing of the addresses inline for convenience, but kept getting pushback from the GUI when I’d try to just add in a client without an address.Turns out, the Django automatic admin interface detects the presence/absence of fields in the inline widget to decide whether or not to validate the input.  In other words, if you’re putting any value into any of your Address’ fields, it will validate them.  Because I was using a “USStateField()” type on my model, it was always putting in a value for the state, hence it was always failing the validation b/c I wasn’t putting in other fields.Man, very frustrating.  Replacing “USStateField()” with a “models.CharField(max_length=2)” did the trick but I don’t think it was clear at all what was happening.Anyone who has solved this more elegantly, please get in touch.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s