Sunday, August 19, 2012

Re: New to dj: relational limitations

Am 15.08.2012 um 01:49 schrieb Russell Keith-Magee:

> For example, in order for the admin to allow
> for an "empty" input, you'll need to set the field as 'blank=True',
> which means the NULL will be converted to a blank string.
>
> This could be addressed at the form level, but it will involve a
> little bit of tinkering (specifically, overriding the to_python method
> on django.forms.CharField). However, once you've written a CharField
> that treats blanks the way you want, you just need to remember to use
> your own form library rather than Django's defaults.
This was easier as expected (+1 for dj; any comments welcome):
--- my modelFields.py:
from django.db import models
from formFields import NULLCharFieldF

class NULLCharFieldM(models.Field):

def formfield(self, **kwargs):
"Returns a FormField which returns an empty string as None instead of ''."
defaults = {'form_class': NULLCharFieldF}
defaults.update(kwargs)
return super(NULLCharFieldM, self).formfield(**defaults)
---
--- my formFields.py:
from django.core import validators
from django.forms.fields import *

class NULLCharFieldF(CharField):

def to_python(self, value):
"Returns a none-empty Unicode object or None."
if value in validators.EMPTY_VALUES:
return None
return smart_unicode(value)
---
Next I will try to adapt things like date interval in days etc.

Axel
---
PGP-Key:29E99DD6 ☀ +49 151 2300 9283 ☀ computing @ chaos claudius

--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate