user19983
user19983

Reputation: 50

Django project sending back Category Object instead of name of that Category

So at the moment when I go to get the view page of my html page Measurements the Category column should show the name of the category West, East, or Volcanoes. However at the moment it is just showing Category object. If there is more then one category object then it will display it as category object, category object instead of West,East

Here are my models:

class Area(models.Model):
    id = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=50)
    longitude = models.FloatField(max_length=100)
    latitude = models.FloatField(max_length=100) 

class Category(models.Model):
    id = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=50)
    description = models.CharField(max_length=100)
    members = models.ManyToManyField('Area')

And here is my function to get that data.

@property
    def category_names(self):
         result = Category.objects.filter(members__id=self.id)
         return ", ".join(str(res) for res in result)

What am I doing wrong? The def category_names is under my Area models so the self.id is from area. I'm sure it has something to do with my function. Always when it comes to django I'm just missing something small but not sure what....

Thanks guys!

Upvotes: 1

Views: 113

Answers (1)

trinchet
trinchet

Reputation: 6933

You need to add __unicode__ method to your Category class:

 class Category(models.Model):
    id = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=50)
    description = models.CharField(max_length=100)
    members = models.ManyToManyField('Area')

    def __unicode__(self):
        return self.name

or return the name of the category when you build the list:

return ", ".join(str(res.name) for res in result)

Upvotes: 1

Related Questions