niloofar
niloofar

Reputation: 2334

__unicode__() method doesn't work well

In models.py:

class POstan(models.Model):
    name = models.TextField()
    shortname = models.TextField()
    map = models.TextField()
    man = models.CharField(max_length=128)
    woman = models.CharField(max_length=128)
    about = models.TextField(blank=True, null=True)
    map_img = models.TextField()

def __str__(self):
    return self.name

class Meta:
    managed = False
    db_table = 'p_ostan'

In views.py:

def select(request):
    list = PMenu.objects.all()
    os = request.META['PATH_INFO']
    os = os[8:]
    items = PShahr.objects.filter(ostan=os)
    farsi = POstan.objects.filter(shortname=os)
return render(request, 'select.html', {'list':list, 'os':os, 'farsi': farsi, 'items': items})

select.html:

{{ farsi }}

The output is this right now:

[<POstan: اصفهان>]

I want the output to be like this:

اصفهان

How should I do that?

Upvotes: 0

Views: 29

Answers (1)

Shang Wang
Shang Wang

Reputation: 25539

You had this display is because you use filter to get farsi, which gives you the result of a queryset. If you print queryset it looks like a list with [] around it. What you need is get:

farsi = POstan.objects.filter(shortname = os)

This would give you a single object and {{ farsi }} would only refer to __unicode__ of a single object.

Upvotes: 1

Related Questions