Question about the __unicode__()

34 views
Skip to first unread message

Igor Korot

unread,
Jan 8, 2014, 6:03:58 PM1/8/14
to django...@googlegroups.com
Hi, ALL,
In the tutorial it is said that I need to create __unicode__() function to properly display the results of my model.
Unfortunately the tutorial is using only one text field and everywhere I looked people are using something related, i.e. first_name+last_name and so they can create a tuple.

What if my strings are not related to each other at all?
For example I might have:

from django.db import models

class Stuff(models.Model):
     Manufacturer = models.CharField(max_length=100)
     Serial_number = models.CharField(max_length=20)

manufacturer and serial_number are not related and I don't want them to be displayed as one field in the output.
So what the __unicode__() function will look like in this case?

Thank you.

Avraham Serour

unread,
Jan 8, 2014, 6:17:07 PM1/8/14
to django...@googlegroups.com
it will be whatever you want, 
you don't actually need to define it but the default implementation is not very useful, but you can override so it behaves like you need
it can show manufacturer+' '+serial, or only one or the other, or even a constant unrelated string like 'Hello mom!'

good luck


--
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/3cb9594d-718f-424c-9d36-25bce866bf2b%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Igor Korot

unread,
Jan 8, 2014, 7:04:11 PM1/8/14
to django...@googlegroups.com
Hi, Avraham,
Well, I'm looking for a way to "properly" override this function.
The problem is that I have a lot more text fields in a table/model than just 2, but they are not related as in my example first and last name.

So what would be the proper implementation?

Because right now following tutorial, I don't see a database text when displaying the records.

Thank you.

Avraham Serour

unread,
Jan 8, 2014, 7:08:20 PM1/8/14
to django...@googlegroups.com
Hi,

What do you mean they are not related? like in relational database related?

In any case it doesn't matter how many fields or even if the data comes from a field, this is just a python function that should return a string, just fetch/calculate whatever data you need and make sure to return a string


--
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.

Igor Korot

unread,
Jan 8, 2014, 7:39:06 PM1/8/14
to django...@googlegroups.com
Hi, Avraham,
So, lets say I write something like this:

[code]
def __unicode__(self):
     return (%s %s) manufacturer, serial_number
[/code]

and in tutorial part 1 I will go to the section "Play with API" I will still see 2 different fields when I issue: "Stuff.objects.all()"?

Because as far as I understand those 2 fields will be displayed as one.


Thank you.


On Wednesday, January 8, 2014 3:03:58 PM UTC-8, Igor Korot wrote:

Babatunde Akinyanmi

unread,
Jan 9, 2014, 1:11:39 AM1/9/14
to Django users

Think about it like this. A class is a box with something inside. The __unicode__ method is putting a label on the box to describe what is inside the box. The label doesn't have to be there and if its there, it can have anything written on it (which should make sense to the person writing it). The purpose of the label is to let anybody know what is inside the box.

Hope that helps

--
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.

Igor Korot

unread,
Jan 9, 2014, 1:40:07 AM1/9/14
to django...@googlegroups.com
Tundebabzy,
I understand that.
But at the same time it should make some sense, otherwise what is the point of having this label (__unicode__() function), right?


Thank you.


On Wednesday, January 8, 2014 3:03:58 PM UTC-8, Igor Korot wrote:

trojactory

unread,
Jan 9, 2014, 5:23:04 AM1/9/14
to django...@googlegroups.com
Igor,

Are you looking for a more meaningful label, say using string interpolation:

def __unicode__(self):
     return "Manufacturer: %s, Serial no: %s" % (manufacturer, serial_number)

Avraham Serour

unread,
Jan 9, 2014, 5:32:55 AM1/9/14
to django...@googlegroups.com
ultimately it should make sense to you (or anyone using the system) so you can identify each object in general before opening it in full


--
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.
Reply all
Reply to author
Forward
0 new messages