Using Unicode in django

48 views
Skip to first unread message

temiloluwa adesina

unread,
Apr 2, 2015, 2:10:28 PM4/2/15
to django...@googlegroups.com
Hello 
I just joined this group and i have problems with representing multiple fields in admin using __unicode__, when i try to call this data it returns just one of them:

class Crop(models.Model):
    name
= models.CharField(max_length=30)
    importance
= models.TextField()
    cultivation
= models.TextField()
   
def __unicode__(self):
       
return u'%s %s %s' % (self.name, self.importance, self.cultivation)

but rather than returning just the data in the importance field, it returns all the data in each separate field together.

Pls i would appreciate anyone's assistance.

Filipe Ximenes

unread,
Apr 2, 2015, 2:57:18 PM4/2/15
to django...@googlegroups.com
Not sure I understood the problem. Are you trying to print only the data in the "importance" field?
If so, try:
class Crop(models.Model):
    name = models.CharField(max_length=30)
    importance = models.TextField()
    cultivation = models.TextField()
    def __unicode__(self):
        return u'%s' % (self.importance,)

--
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/504bf928-7259-4d17-994c-115e98a78a4b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
  
Filipe Ximenes
+55 (81) 8245-9204
Vinta Software Studio
http://www.vinta.com.br

temiloluwa adesina

unread,
Apr 2, 2015, 3:56:32 PM4/2/15
to django...@googlegroups.com
Thanks for your reply i found another way to do it, i could just make it work like this:

b= Crop.objects.all()
nm
= b.name
imp
= b.importance
cuv
= b.cultivation

realized this would help me retrieve the individual data that i wanted to get.

Mike Dewhirst

unread,
Apr 3, 2015, 4:54:24 AM4/3/15
to django...@googlegroups.com
On 3/04/2015 12:01 AM, temiloluwa adesina wrote:
> Hello
> I just joined this group and i have problems with representing multiple
> fields in admin using __unicode__, when i try to call this data it
> returns just one of them:
>
> |
> classCrop(models.Model):
> name =models.CharField(max_length=30)
> importance =models.TextField()
> cultivation =models.TextField()
> def__unicode__(self):
> returnu'%s %s %s'%(self.name,self.importance,self.cultivation)
> |
>
> <https://lh3.googleusercontent.com/-2cHWwMHqUnY/VR09L1-ntvI/AAAAAAAAAGY/AdVaYuoL6As/s1600/Screenshot%2B%2840%29.png>
>
> but rather than returning just the data in the importance field, it
> returns all the data in each separate field together.

That __unicode__() return value is a concatenation of the three named
fields and I assume that is what you want. When the object is returned
its name is mentioned first (Yam) followed by a space followed by
whatever data is in the importance field followed by a space then
cultivation

Your screenshot seems to be showing that.

If all you want is the importance info, just make __unicode__() return
that and nothing else.

Whatever is returned by __unicode__ is the object's identification.

You seem to have a good handle on that so I suspect I haven't understood
your question.

Mike

>
> Pls i would appreciate anyone's assistance.
>
> --
> 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
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> <https://groups.google.com/d/msgid/django-users/504bf928-7259-4d17-994c-115e98a78a4b%40googlegroups.com?utm_medium=email&utm_source=footer>.

Luis Zárate

unread,
Apr 4, 2015, 10:19:32 PM4/4/15
to django...@googlegroups.com
Are you runing python 2.7.x ?  Think in python 3 compatibility  and solve your problem

This code can help you.

from __future__ import unicode_literals
from django.utils.encoding import python_2_unicode_compatible

@python_2_unicode_compatible
class MyModel(models.Model):
    name = models.CharField(max_length=300)
    importance = models.TextField()

    def __str__(self):
        return self.importance





To post to this group, send email to django...@googlegroups.com
--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.

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



--
"La utopía sirve para caminar" Fernando Birri


Reply all
Reply to author
Forward
0 new messages