3

I've searched all over, but couldn't find a good example.

I have two models, one of which has a ManyToManyField. I'm trying to pass a QuerySet that needs information through the ManyToManyField into a custom serializer and return a JSON response.

model.py

class Company(models.Model):
    """Company in database."""

    founded = models.DateField(null=True)
    name = models.CharField(max_length=60)
    series = models.CharField(max_length=10, null=True)
    valuation = models.DecimalField(max_digits=20, decimal_places=5, null=True)
    description = models.TextField(null=True)

    def __unicode__(self):
        """Return human-readable representation of company for debugging."""

        return '<{}>'.format(self.name)


class Metric(models.Model):
    """Metrics for companies."""

    name = models.CharField(max_length=30)
    end_date = models.DateField()
    start_date = models.DateField()
    company = models.ManyToManyField(Company)
    value = models.IntegerField()

    def __unicode__(self):
        """Return human-readable representation of metric for debugging."""

        return '<{}, {}>'.format(self.name, self.company)

views.py

class ClientAPI:

@classmethod
def _serialize(cls, objects):
    def create_company_map():
        return {c.id: c.name for c in Company.objects.all()}

    # Replace company ids with company names
    def map_company(obj, company_map):
        if 'company' in obj:
            obj['company'] = company_map[obj['company']]
        return obj

    company_map = create_company_map()
    raw = serializers.serialize('python', objects)
    res = [dict(map_company(obj['fields'], company_map)) for obj in raw]
    return res

@classmethod
def get_metrics(cls, request):
    all_metrics = Metric.objects.all().order_by('end_date')
    serialized_metrics = ClientAPI._serialize(all_metrics)

    return JsonResponse(serialized_metrics, safe=False, json_dumps_params=None)

@classmethod
def get_metrics_by_company(cls, request, company_name):
    metrics_by_company = Metric.objects.filter(company__name=company_name).order_by('end_date')
    serialized_metrics_by_company = ClientAPI._serialize(metrics_by_company)

    return JsonResponse(serialized_metrics_by_company, safe=False, json_dumps_params=None)

I can't change the serializer, cause I have other methods that depend on it. When I run this code, I get:

TypeError: unhashable type: 'list'

Any insight into how I can resolve this is greatly appreciated! Just in case, this is the traceback.

Traceback (most recent call last):
  File "/Users/jackiehuynh/anaconda/lib/python2.7/site-packages/django/core/handlers/base.py", line 149, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/jackiehuynh/anaconda/lib/python2.7/site-packages/django/core/handlers/base.py", line 147, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/jackiehuynh/Lytmus/question_1/client/views.py", line 167, in get_metrics
    serialized_metrics = ClientAPI._serialize(all_metrics)
  File "/Users/jackiehuynh/Lytmus/question_1/client/views.py", line 65, in _serialize
    res = [dict(map_company(obj['fields'], company_map)) for obj in raw]
  File "/Users/jackiehuynh/Lytmus/question_1/client/views.py", line 60, in map_company
    obj['company'] = company_map[obj['company']]
TypeError: unhashable type: 'list'

2 Answers2

0

Since company is a m2m field, it seems obj['company'] in following statement will be a list:

obj['company'] = company_map[obj['company']]

and, you are trying to get some value from company_map dict by passing a list as key which leads to this error.

You can use list comprehension to get the company names from the ids:

obj['company'] = [company_map.get(idx, None) for idx in obj['company']]

You can also follow this post for detailed explanation, and if you want you can try this yourself in a shell:

In [1]: x = {'a': 1}

In [2]: x[[1, 2]]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-7-1032576b46f9> in <module>()
----> 1 x[[1, 2]]

TypeError: unhashable type: 'list'
Community
  • 1
  • 1
AKS
  • 18,983
  • 3
  • 43
  • 54
  • I did figure that part out, but I'm a little stumped on how to resolve it. Any ideas? The only thing I can't change is the serializer, but everything else in views.py and model.py are changeable. – sabellachan Mar 08 '16 at 05:53
0

Nevermind, I may have asked a bad question. Turns out what I wanted to do was actually use a ForeignKey, not ManyToManyField. Thanks for the help anyway!