how do i return an int as the returned object for admin panel

210 views
Skip to first unread message

G Z

unread,
Jul 14, 2014, 5:38:19 PM7/14/14
to django...@googlegroups.com
how do I fix this?


class Vm_licenses(models.Model):
   vm_license_id = models.BigIntegerField(primary_key = True, editable = False, db_column = 'vm_license_id')
   license= models.ForeignKey(License,  on_delete = models.PROTECT)
   vm= models.ForeignKey(Vm,  on_delete = models.PROTECT)

   class Meta:
      managed = False
      db_table = 'vm_licenses'
   
   def __unicode__(self):  # Python 3: def __str__(self):
      return self.vm_id

Request Method:GET
Request URL:http://127.0.0.1:8000/admin/portal/vm_licenses/
Django Version:1.6.5
Exception Type:AttributeError
Exception Value:
'int' object has no attribute 'encode'
Exception Location:/usr/local/lib/python2.7/dist-packages/django/db/models/base.py in __str__, line 430
Python Executable:/usr/bin/python
Python Version:2.7.5

Tom Evans

unread,
Jul 14, 2014, 6:30:44 PM7/14/14
to django...@googlegroups.com
The __unicode__ method must return a unicode string.

The function unicode(), or string formatting would suffice.

Cheers

Tom


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/06de81a6-131a-4e6e-9f24-c370cdd14076%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Aeh. ABID

unread,
Jul 14, 2014, 6:31:36 PM7/14/14
to django...@googlegroups.com
In your def __unicode__ convert the current returned int to string :

 def __unicode__(self):  # Python 3: def __str__(self):

Tom Evans

unread,
Jul 14, 2014, 6:40:46 PM7/14/14
to django...@googlegroups.com
On Mon, Jul 14, 2014 at 11:31 PM, Aeh. ABID <aeh....@gmail.com> wrote:
>
> In your def __unicode__ convert the current returned int to string :
>
>> def __unicode__(self): # Python 3: def __str__(self):
>> return str(self.vm_id)
>

Stop, you are making my eyes bleed!

It is really fairly important that you return a unicode object from
the __unicode__ method and not a str object.
__unicode__ is the method that python calls when python wants to turn
your object in to a unicode representation of it. The method takes no
arguments, and the ONLY post-condition is that it returns a unicode
object - it should not be too taxing to get it right!

If you do not return a unicode object, strange and unpredictable
things can happen (like AttributeError("'int' object has no attribute
'encode'")).

Cheers

Tom

monoBOT

unread,
Jul 15, 2014, 6:46:51 AM7/15/14
to django...@googlegroups.com
The __unicode__ method is meant to return a human readable representation of the model object as a unicode.
EG:

def __unicode__(self):
    return u'%(id)s' % {'id': self.vm__id}

althought not very human readable is a correct __unicode__ method.

The reason behind you wanting a int number in the __unicode__ method is strange for me/us.
Also, creating a method just to get the object id doesnt seem too bright.

why not just object.vm__id ?



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

For more options, visit https://groups.google.com/d/optout.



--
monoBOT
Visite mi sitio(Visit my site): monobotsoft.es/blog/
Reply all
Reply to author
Forward
0 new messages