Model - def __unicode__(self) - doesn't work

155 views
Skip to first unread message

Przemek Ciborowski

unread,
Aug 25, 2014, 8:47:10 PM8/25/14
to django...@googlegroups.com
Hello guys,

I'm really beginner in django.
I have extremely simple example:

class Vlan(models.Model):
    name = models.CharField(max_length=30, name='Vlan name')
    number = models.IntegerField(default=0, name='Vlan number')

    def __unicode__(self):
        return self.name

When I'm trying to use admin interface, I got this error:

AttributeError at /admin/networks/vlan/add/

'Vlan' object has no attribute 'name'
Request Method:POST
Request URL:http://127.0.0.1:8000/admin/networks/vlan/add/
Django Version:1.6.6
Exception Type:AttributeError
Exception Value:
'Vlan' object has no attribute 'name'
Exception Location:D:\Scripts\Mercury\networks\models.py in __str__, line 10
Python Executable:C:\Python\Main\Scripts\python.exe
Python Version:2.7.8

I'm sure that it was working a few days ago. And in meantime I did django upgrade.
What I'm doing wrong, and how I can fix it?

Thanks in advance.
Przemek.

Babatunde Akinyanmi

unread,
Aug 25, 2014, 10:33:09 PM8/25/14
to Django users

Hi Przemek,
I'm not sure there's a name attribute for Field and its subclasses.
Does removing the name attribute work?

--
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/50470bc3-c7a0-4afd-b655-f7a3cf8304a0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mario Gudelj

unread,
Aug 25, 2014, 10:49:01 PM8/25/14
to django...@googlegroups.com

Your problem is in the add view. Post your view code

--

monoBOT

unread,
Aug 26, 2014, 8:24:07 AM8/26/14
to django...@googlegroups.com
Be sure to returm a unicode object like this:

    def __unicode__(self):
        return u"%s" % self.name



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



--
monoBOT
Visite mi sitio(Visit my site): monobotsoft.es/blog/

Przemek Ciborowski

unread,
Aug 26, 2014, 9:37:44 AM8/26/14
to django...@googlegroups.com
Hi, thanks for your reply.

I forgot to mention that this problem occurs with admin interface.

Regards
Przemek.

Przemek Ciborowski

unread,
Aug 26, 2014, 9:39:13 AM8/26/14
to django...@googlegroups.com
Hi, thanks for your reply.

It didn't help, I also trid return unicode(self.name)

Regards
Przemek.

Przemek Ciborowski

unread,
Aug 26, 2014, 12:22:02 PM8/26/14
to django...@googlegroups.com
So to be more clearly, let create new django project with one application for example with name networks.
Add those lines to the networks/models.py:

class Vlan(models.Model):

    name = models.CharField(max_length=30, name='Vlan name')

    def __unicode__(self):
        return self.name

add those two lines to networks/admin.py

from .models import *
admin.site.register(Vlan)

It should be (and was) enough to run admin interface. Unfortunately when I'm trying to add new vlan or list current vlans I get:
-------------------

AttributeError at /admin/networks/vlan/add/

'Vlan' object has no attribute 'name'
Request Method:POST
Request URL:http://127.0.0.1:8000/admin/networks/vlan/add/
Django Version:1.6.6
Exception Type:AttributeError
Exception Value:
'Vlan' object has no attribute 'name'
Exception Location:C:\Scripts\Test1\networks\models.py in __unicode__, line 11
Python Executable:C:\Python\Main\Scripts\python.exe
Python Version:2.7.8
Python Path:
['C:\\Scripts\\Test1',
 'C:\\Scripts\\Test1',
 'C:\\Windows\\SYSTEM32\\python27.zip',
 'C:\\Python\\Main\\DLLs',
 'C:\\Python\\Main\\lib',
 'C:\\Python\\Main\\lib\\plat-win',
 'C:\\Python\\Main\\lib\\lib-tk',
 'C:\\Python\\Main\\Scripts',
 'C:\\Python27\\Lib',
 'C:\\Python27\\DLLs',
 'C:\\Python27\\Lib\\lib-tk',
 'C:\\Python\\Main',
 'C:\\Python\\Main\\lib\\site-packages']
Server time:Tue, 26 Aug 2014 14:12:06 +0200



-----------------------

More information here : http://dpaste.com/26QQPKZ

Look this is code from first part of official django tutorial.
When I delete def __unicode__(self) method from models file, it works fine but in vlan list I get Vlan object, Vlan object.... etc instead of names.


Regards
Przemek.

James Schneider

unread,
Aug 26, 2014, 1:38:33 PM8/26/14
to django...@googlegroups.com
Try changing the 'name' argument to 'verbose_name' like so:

name = models.CharField(max_length=30, verbose_name='Vlan name')

I don't believe model fields accept 'name' as a keyword arg. This would also explain the slightly confusing error due to the overlap of 'name' appearing in multiple spots.



-James
--
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/01dd14d6-43d7-4519-bffb-b6d33a083f54%40googlegroups.com.

Babatunde Akinyanmi

unread,
Aug 26, 2014, 1:47:55 PM8/26/14
to Django users


On 26 Aug 2014 14:38, "James Schneider" <jrschn...@gmail.com> wrote:
>
> Try changing the 'name' argument to 'verbose_name' like so:
>
> name = models.CharField(max_length=30, verbose_name='Vlan name')
>
> I don't believe model fields accept 'name' as a keyword arg.
This would also explain the slightly confusing error due to the overlap of 'name' appearing in multiple spots.
>

Just like I pointed out in my earlier mail. Remove the name argument or change it to verbose_name as recommended by James

> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciWRN00qpz1SRvzgC_k0av92MQyPsN0TSU1CBdBh-QFbWw%40mail.gmail.com.

Babatunde Akinyanmi

unread,
Aug 26, 2014, 1:49:56 PM8/26/14
to Django users


On 25 Aug 2014 23:32, "Babatunde Akinyanmi" <tunde...@gmail.com> wrote:
>
> Hi Przemek,
> I'm not sure there's a name attribute for Field and its subclasses.
> Does removing the name attribute work?
>

Oops. I meant argument not attribute

Przemek Ciborowski

unread,
Aug 26, 2014, 2:18:54 PM8/26/14
to django...@googlegroups.com
Thank you a lot!

James & Tundebabzy - Thanks:)

It was the mistake.

Regards
Przemek
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

Tom Evans

unread,
Aug 26, 2014, 6:13:24 PM8/26/14
to django...@googlegroups.com
On Tue, Aug 26, 2014 at 12:22 PM, Przemek Ciborowski <cib...@gmail.com> wrote:
>
> So to be more clearly, let create new django project with one application for example with name networks.
> Add those lines to the networks/models.py:
>
> class Vlan(models.Model):
>
> name = models.CharField(max_length=30, name='Vlan name')

"name" is not an argument for CharField - do you mean verbose_name?

I suspect this error causes Vlan.name to not be defined.

Cheers

Tom
Reply all
Reply to author
Forward
0 new messages