luddite
luddite

Reputation: 430

Displaying list from getlist django

I have a users click a series of checkboxes to identify which areas a certain place is related to (i.e, entertainment, sports etc) , which I then retrieve with:

    areas_related=request.POST.getlist('areas_related')

When I am displaying those values in a different view of what areas different places are related to I am using:

                    {% for service in services %}
                    {{service.areas_related}}
                    {% endfor %}

... But it is displayed as:

[u'Education', u'Food'] 

.. If I try to make a for loop to go through the list, each individual character gets displayed, one line at a time. I.e., [ u ' E etc.Is there a filter I can use to not display [u', etc.?

Any help would be appreciated.

Upvotes: 0

Views: 1297

Answers (1)

miki725
miki725

Reputation: 27861

What is probably going on here is that you are storing the areas_related as a string.

So you probably have a model like:

class Service(models.Model):
    ...
    areas_related = models.CharField(max_length=<length_here>)

and then in the view you do this:

def foo_view(request, ...):
    ...
    service = <retrieve Service here>
    areas_related = request.POST.getlist('areas_related')
    service.areas_related = areas_related
    service.save()
    ...

So what is going on here is that request.POST.getlist('areas_related') returns a Python list. However then you store that value into a string field of your model. As a result, Django tries to convert the given list to a string, which it then stores in the db; and when you convert Python list to a string, the result is a string representation of the list:

>>> a = [u'foo', u'foo2']
>>> unicode(a)
u"[u'foo', u'foo2']"

To solve this, you can do one of these things:

Foreign Key

Instead of storing all the areas_related as a single field within your model, store those as foreign keys. This I think is a better way because then you can do SQL aggregation and is just considered more flexible compared to the next method.

# models.py
class AreaRelated(models.Model):
     service = models.ForeignKey('Service', related_name='areas_related')
     areas_related = models.CharField(max_length=<length_here>)

class Service(models.Model):
    ...

# views.py
def foo_view(request, ...):
    ...
    service = <retrieve Service here>
    areas_related = request.POST.getlist('areas_related')
    for a in areas_related:
         service.areas_related.create(areas_related=a)
    ...

# template
{% for service in services %}
    {% for area_related in service.areas_related.all() %}
        {{ area_related }}
    {% endfor %}
{% endfor %}

Encoded string

Store the list in the areas_related model field as a string by encoding it somehow, and then on retrieval decode to get the list. This method is nice because everything gets stored in db in one column, however then you can't really do any queries on the areas_related because it will store an encoded string of multiple values instead of just one.

# models.py
import json
class Service(models.Model):
    ...
    areas_related = models.CharField(max_length=<length_here>)

    def get_areas_related(self):
         return json.loads(self.areas_related)

# views.py
import json
def foo_view(request, ...):
    ...
    service = < retrieve Service here>
    areas_related = request.POST.getlist('areas_related')
    service.areas_related = json.dumps(areas_related)
    service.save()
    ...

# template
{% for service in services %}
    {% for area_related in service.get_areas_related %}
        {{ area_related }}
    {% endfor %}
{% endfor %}

Upvotes: 2

Related Questions