xzased
xzased

Reputation: 51

Django view to retrieve data from model returns object name only

I have a model with data in it defined like this:

class SyncJob(models.Model):
  date = models.DateTimeField()
  user = models.ForeignKey(User, unique=False)
  source = models.CharField(max_length=3, choices=FS_CHOICES)
  destination = models.CharField(max_length=3, choices=FS_CHOICES)
  options = models.CharField(max_length=10, choices=OPTIONS)

  def _unicode_(self):
    return u'%s %s %s' % (self.date, self.source, self.destination)

And I have a view to retrieve data:

def retrieve(request):
  sync = SyncJob.objects.get(id=02)
  return render_to_response('base.html', {'sync': sync})

But when rendering the page I only get: SyncJob object Instead of getting the date, source and destination info. How can I make it so I get this data?

Upvotes: 1

Views: 3230

Answers (1)

pmalmsten
pmalmsten

Reputation: 426

Watch the naming of special methods:

def _unicode_(self):
    ...

should be:

def __unicode__(self):
    ...

Python special methods have two underscores on each end of the name.

Upvotes: 2

Related Questions