I’m getting attribute errors “‘NoneType’ object has no attribute ‘strip'” when trying to coerce form input data to uppercase. My forms code (clean method borrowed from another programmer) is:
class PostPageForm(ModelForm):
class Meta:
model = PostPage
def clean(self):
return dict([(k, v.strip().upper()) for k, v in self.cleaned_data.items()])
and my model:
class PostPage(models.Model):
client = models.CharField(max_length=50, choices=CLIENT_CHOICES)
job_number = models.CharField(max_length=30, unique=True, blank=False, null=False)
job_name = models.CharField(max_length=64, unique=False, blank=False, null=False)
page_type = models.CharField(max_length=50, default='POST')
create_date = models.DateField(("Date"), default=datetime.date.today)
contact = models.ForeignKey(UserProfile)
contact2 = models.ForeignKey(UserProfile, related_name='+', blank=True, null=True)
contact3 = models.ForeignKey(UserProfile, related_name='+', blank=True, null=True)
contact4 = models.ForeignKey(UserProfile, related_name='+', blank=True, null=True)
def __unicode__ (self):
return u'%s %s %s' % (self.client, self.job_number, self.job_name)
class Admin:
pass
class Meta:
permissions = (
('view_postpage', 'View postpage'),
)
I think the override of the “clean” method in the forms code needs to be reconfigured based on the return value of the model. I’m just not sure how.
This is not a good approach, to do it: it tries to call
.upperfor every object incleaned_data, and it can beint,Noneand any other type.The solution is something like:
But why do you need this? May be it’s better to store strings as is and just display them uppercased?