On Sep 15, 7:33 pm, SnappyDjangoUser <[EMAIL PROTECTED]> wrote:
> Hi Folks,
>
> I am receiving an obscure TemplateSyntaxError (Caught an exception
> while rendering: coercing to Unicode: need string or buffer, long
> found ) when rending a ModelForm that has a foreign key back to a
> specific table.  My template  correctly renders forms for all models
> except for this one case and I have not been able to figure out why.
>
> AssetForm correctly renders fields on its own.  The problem occurs
> when rendering the RackForm which has a foreign key to the Asset
> model.
>
> Other models have foreign key relationships and those fields are
> rendered correctly. The only clue I have is that the model giving me
> trouble, Asset, has an unsigned IntegerField of length 12
> (Asset_Number).  See below for model information:
>
> class Asset(models.Model):
>     idAsset = models.IntegerField(primary_key=True)
>     Asset_Number = models.PositiveIntegerField(max_length=12,
> verbose_name='Asset Number')
>     class Meta:
>         db_table = 'Asset'
>     def __unicode__(self):
>         return self.Asset_Number
>
> class Rack(models.Model):
>     idRack = models.IntegerField(primary_key=True)
>     SerialNumber = models.CharField(max_length=135)
>     RackLocation = models.CharField(blank=True, max_length=30)
>     Vendor_idVendor = models.ForeignKey(Vendor,
> db_column='Vendor_idVendor')
>     Facility_idFacility = models.ForeignKey(Facility, null=True,
> db_column='Facility_idFacility', blank=True)
>     Asset_idAsset = models.ForeignKey(Asset,
> db_column='Asset_idAsset', max_length=12)
>     class Meta:
>         db_table = 'Rack'
>     def __unicode__(self):
>         return self.RackLocation
>
> class AssetForm(ModelForm):
>     class Meta:
>         model = Asset
>         exclude = ['idAsset']
>
> class RackForm(ModelForm):
>     class Meta:
>         model = Rack
>         exclude = ['idRack']
>
> Template Snippet with the error:
> 18       <table>
> 19      {% for field in form %}
> 20      <tr>
> 21      <th>{{ field.label_tag }}{% if field.field.required %}*{% endif
> %}</th>
> 22      <td>{{ field }}{% if field.help_text %}<br>{{ field.help_text }}</
> br>{% endif %}</td>
> 23      {% if field.errors %}<td class="myerrors">{{ field.errors }}</
> td>{% endif %}
> 24      </tr>
> 25      {% endfor %}
> 26      </table>
>
> Django says that the error is occuring at line 19, but I have not been
> able to figure out why.  Any ideas?  Like I said above, this template
> works just fine for most cases.  The only case that gives me problems
> is when rendering the RackForm (which has a foreign key to Asset).
>
> Thanks in advance for the help...


The error is fairly descriptive: it's trying to convert something to
unicode, but is only receiving a Long integer value.

The problem lies in the __unicode__ method of the Asset field. This
method isn't returning a unicode value as it should, but the contents
of the Asset_Number field - which is a PostiveInteger. Django uses the
__unicode__ method to display the values in the drop-down field for
Asset_idAsset. (Weird field names, by the way: Django/Python
convention is to use lower_case_with_underscores for field names, and
CapsCase for class/model names.)

You should rewrite it along these lines:

def __unicode__(self):
    return u'%s' % self.Asset_Number

--
DR
--~--~---------~--~----~------------~-------~--~----~
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