Adding a new record in Django Admin gives persistent error

I'm just starting to mess around with Django. I created a new project and a new app. In that app I created a model and activated the admin. This seemed to work fine. I then wanted to add a couple new records to the database using the admin. In the first three tables this went fine, but in the fourth one (called 'locations') I get this error saying: 'tuple' object has no attribute 'encode'. The full error is here on pastebin: http://pastebin.com/WjZat6NN

The strange thing is that when I now go back to the general admin page and want to click the table on which I just got the error, I get the error as well (so without trying to add anything).

My question: why is this happening? Maybe there is something wrong with my models.py, so I pasted it below this message as well.

All tips are welcome!

from django.db import models

# Create your models here.
class countries(models.Model):
    country = models.CharField(max_length=100)
    def __unicode__(self):
        return self.country

class organisationTypes(models.Model):
    organisationType = models.CharField(max_length=100)
    def __unicode__(self):
        return self.organisationType

class organisations(models.Model):
    organisationName = models.CharField(max_length=200)
    organisationType = models.ForeignKey(organisationTypes)
    countryofOrigin = models.ForeignKey(countries)
    def __unicode__(self):
        return self.organisationName

class locations(models.Model):
    organisation = models.ForeignKey(organisations)
    countryofLocation = models.ForeignKey(countries)
    telNr = models.CharField(max_length=15)
    address = models.CharField(max_length=100)
    def __unicode__(self):
        return self.organisation, self.countryofLocation, self.telNr, self.address

Answers


Here:

def __unicode__(self):
        return self.organisation, self.countryofLocation, self.telNr, self.address

You're returning a tuple. It expects a single string.

Change it to something like this:

def __unicode__(self):
        return "%s - %s - %s - %s" % (self.organisation self.countryofLocation, self.telNr, self.address)

The problem is most likely with this line...

return self.organisation, self.countryofLocation, self.telNr, self.address

...where you're returning a tuple from a __unicode__ method. You'll need to return a string object instead, although it's unclear what it should be. Perhaps...

return ', '.join((self.organisation, self.countryofLocation, self.telNr, self.address))

...?


You can only return a string to acts as a representative to a model instance.

So better use

return self.organisation + '-'+ self.countryofLocation + '-'+self.telNr+'-'+self.address

Need Your Help

Interview follow up on Class Design by avoiding diamond case

java design uml

Recently I have been asked design question. There are two classes Horse and Bird. I need to design new class called Unicorn which is having all the methods of class Horse and Bird.

PHP preg_replace: remove all 2 char words & 3 char ending in s

php regex

Sorry for being dumb when it comes to RegEx, but I'm just having an issue with this:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.