0
def participant_specific_donation(self, participant):
    donations = Donation.objects.filter(participant = participant)

    helper = {}

    for donation in donations:
        ##helper['donation_date'] = donation.date
        helper['donation_amount'] = donation.amount
    return helper

def participant_specific(request, participant):
helper = RelayFunctions()
info = helper.participant_specific_donation(participant)
info1 = helper.participant_specific_milestone(participant)

data = [ 'participant_specific_donation' : info , 'participant_specific_milestone' : info1 ]
json_serializer = serializers.get_serializer("json")()
response = json_serializer.serialize(data, ensure_ascii=False)
return HttpResponse(response, mimetype="application/json")

Error: 'dict' object has no attribute '_meta' Does this have to do with how I call the dictionary? I wanted to combine two objects into an one. Then parse it to json.

Here's the traceback.

Traceback:
 File "/home/vtrelayc/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response
   111.                         response = callback(request, *callback_args, **callback_kwargs)
 File "/home/vtrelayc/projects/relay/relayapp/views.py" in participant_specific
   192.     response = json_serializer.serialize(data, ensure_ascii=False)
 File "/home/vtrelayc/lib/python2.6/site-packages/django/core/serializers/__init__.py" in serialize
  98.     s.serialize(queryset, **options)
File "/home/vtrelayc/lib/python2.6/site-packages/django/core/serializers/base.py" in serialize
  46.             concrete_model = obj._meta.concrete_model

Exception Type: AttributeError at /participants/specific/1/
Exception Value: 'dict' object has no attribute '_meta'

def participant_specific(request, participant):
helper = RelayFunctions()
info = helper.participant_specific_donation(participant)
info1 = helper.participant_specific_milestone(participant)
data = [ 'participant_specific_donation' : info , 'participant_specific_milestone' : info1 ]
json_serializer = serializers.get_serializer("json")()
response = json_serializer.serialize(data, ensure_ascii=False) ...
return HttpResponse(response, mimetype="application/json")
sccrthlt
  • 3,974
  • 5
  • 20
  • 24
  • The `data = [ ...` line will yield a syntax error. – Dave Feb 10 '13 at 02:09
  • 1
    Is this the real code? data = ['foo':bar] looks weird. Is this an array or a dict? – t-8ch Feb 10 '13 at 02:10
  • could you post your exact traceback, and the code around the location the traceback references? – dm03514 Feb 10 '13 at 02:16
  • t-8ch: data = { 'obj1' : obj1, 'obj2' : obj2 } json.dumps(data) Came from this: http://stackoverflow.com/questions/1096554/combining-two-json-objects-in-to-one – sccrthlt Feb 10 '13 at 02:20

1 Answers1

1

There are two problems that I see in the code snippet.

First, logical issue here :

for donation in donations:
    ##helper['donation_date'] = donation.date
    helper['donation_amount'] = donation.amount

If donations has multiple elements then you will end up with only the last element in the list, as you are looping over the donations and assigning the value every time to helper.

I think you want to calculate the total amount, which you can do as shown :

helper['donation_amount'] += donation.amount

Second, the line :

data = [ 
    'participant_specific_donation': info,
    'participant_specific_milestone': info1 
]

will give you a syntax error.

It appears that you need a dictionary over here. The correct way to intialize a dictionary is by using the { } braces :

data = { 
    'participant_specific_donation': info, 
    'participant_specific_milestone': info1
}

If you want all the donations in a single place, you can make a list first :

donation_amount = [donation for donation in donations]

and then assign it to the helper dictionary :

helper['donation_amount'] = donation_amount
kdazzle
  • 4,190
  • 3
  • 24
  • 27
asheeshr
  • 4,088
  • 6
  • 31
  • 50
  • Thank you AshRj!! Making that a dictionary works BUT I do want every element in 'donations'. So I would make an array in a dictionary to do that? – sccrthlt Feb 10 '13 at 02:36
  • I know this is 2 years old, but do you mean `donation_amounts = [donation.amount for donation in donations]`? Your list comprehension as written doesn't do anything but make a copy of `donations`. – Two-Bit Alchemist Aug 05 '15 at 21:37