I have a model like this:

class OrderProfile(models.Model):
        cartid = models.CharField(max_length=128, null=True, blank=True)
        orderno = models.CharField(max_length=48, null=True, blank=True)
        userid = models.CharField(max_length=128, null=True, blank=True)
        userfirstname = models.CharField(max_length=128, null=True,
verbose_name='Name')
        userlastname = models.CharField(max_length=128, null=True,
verbose_name='Name')
        address1 = models.CharField(max_length=128, null=True,
verbose_name='Address')
        address2 = models.CharField(max_length=128, null=True, blank=True,
verbose_name='')
        address3 = models.CharField(max_length=128, null=True,
verbose_name='Locality')
        towncity = models.CharField(max_length=128, null=True,
verbose_name='Town/City')
        adminregion = models.CharField(max_length=128, null=True,
verbose_name='District/County', blank=True)
        postcode = models.CharField(max_length=10, null=True,
verbose_name='Postcode')
        telno = models.CharField(max_length=20, null=True,
verbose_name='Telephone')
        email = models.CharField(max_length=128, verbose_name='Email',
null=True)
        def __unicode__(self):
                return self.orderno

I have a ModelForm derived from it like this:

class OrderForm(ModelForm):
        address1 = forms.CharField(widget=forms.TextInput(attrs={ 'size':
'40' }))
        address2 = forms.CharField(widget=forms.TextInput(attrs={ 'size':
'40' }), required=False)
        address3 = forms.CharField(widget=forms.TextInput(attrs={ 'size':
'40' }))
        towncity = forms.CharField(widget=forms.TextInput(attrs={ 'size':
'40' }))
        adminregion = forms.CharField(widget=forms.TextInput(attrs={ 'size':
'40' }))
        class Meta:
                model = OrderProfile
                exclude = ('cartid', 'orderno', 'userid')

as an aside, and which doesn't appear to be documented, I had to add
'required=False' to the address2 widget as setting it overrides the
settings in the model, which I suppose is correct on one level, but
for me isn't expected behaviour. But I'm not interested in that now.

The code to process the form is simple:

        if request.method == 'POST':
                orderform = OrderForm(request.POST)
                if orderform.is_valid():
                        preorder = orderform.save(commit=False)
                        orderno  = '%04d' % (preorder.id)
                        preorder.orderno = orderno
                        preorder.cartid = cartid
                        preorder.save()

I do the uncommited save to get pk to pad it for an order number.
However, at orderform.save, I am now getting the eternally helpful
error

<type 'exceptions.TypeError'>: coercing to Unicode: need string or
buffer, NoneType found.

The POST data I am sending is correct and the error occurs whether
address2 is empty or not. I can replicate this either in a form, where
it fails because preorder.id returns the error, or on the command
line, where the traceback gives no particular help with what is
failing. None of the excluded fields in the form are required in the
db table (in fact I've modified that so that all fields except id are
null). I can see why the error might happen, but I'm not sure if I'm
missing something. Any suggestions?

Thanks
Simon
--~--~---------~--~----~------------~-------~--~----~
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