djangoapache-flexdjango-nonrelpyamf

500 error when returning Django object with Pyamf to Flex


I'm able to return django models that have only CharFields/Dates/Integers, but now I'm trying to return models that have ForeignKey properties and I'm getting this error in Flex in my NetStatusEvent.NET_STATUS onError event handler:

m_info  Object (@16491fe9)  
code    "NetConnection.Call.Failed" 
description "HTTP: Status 500"  
details "http://127.0.0.1:8000/gateway/"    
level   "error" 

Here are the models that matter in models.py:

class RewardActBase(models.Model):
    user = models.ForeignKey(User)
    start_date = models.DateTimeField(blank=True, null=True)
    progress_value = models.IntegerField(default=0)
    coupon_act = models.ForeignKey(CouponAct)

    class Meta:
        abstract = True


class ChallengeAct(RewardActBase):
    challenge = models.ForeignKey(Challenge)

    def __unicode__(self):
        return self.challenge.title'

class CouponAct(models.Model):
    coupon = models.ForeignKey(Coupon)
    earned_date = models.DateTimeField(blank=True, null=True)
    redeemed_date = models.DateTimeField(blank=True, null=True)
    expiration_date = models.DateTimeField(blank=True, null=True)

    def __unicode__(self):
        return self.coupon.title

Then when I want to get retrieve these object via pyamf, this is the method I'm using, which is giving me the error I listed above:

@login_required
def get_challenge_act(http_request, location_id):
    user = http_request.user

    c = ChallengeAct();
    c.challenge = Challenge.objects.select_related().get(id=1)
    c.start_date = datetime.now()
    c.progress_value = 1
    c.user = user
    new_coupon_act = CouponAct()
    new_coupon_act.coupon = Coupon.objects.select_related().get(id=c.challenge.coupon.id)
    new_coupon_act.earned_date = datetime.now()
    new_coupon_act.save()
    c.coupon_act = new_coupon_act
    c.save()

    return c

The interesting thing is that if I change my get_challenge_act method to return a property of the ChallengeAct object, I don't get the error. So I can return properties or objects that belong to the ChallengeAct, but not the ChallengeAct itself. For example, the following code returns a Challenge object with no errors:

    return c.challenge

So it appears that there is some problem returning a Django model with foreginkey models as properties? Am I doing something wrong?


Solution

  • By process of elimination, I found that it was the User object on the ChallengeAct that was causing the problem, and I got the ambiguous 500 error to go away by setting the user object to None after saving and right before returning.

    @login_required
    def get_challenge_act(http_request, location_id):
    user = http_request.user
    
    c = ChallengeAct();
    c.challenge = Challenge.objects.select_related().get(id=1)
    c.start_date = datetime.now()
    c.progress_value = 1
    c.user = user
    new_coupon_act = CouponAct()
    new_coupon_act.coupon = Coupon.objects.select_related().get(id=c.challenge.coupon.id)
    new_coupon_act.earned_date = datetime.now()
    new_coupon_act.save()
    c.coupon_act = new_coupon_act
    c.save()
    c.user = None
    
    return c
    

    I'd love to hear why that happened though. Anyone have any ideas?

    --update-- I found that I can see what the actual 500 error is by looking at the terminal log, after running my runserver command. So the actual error was:

    Could not import "cpyamf.amf3": Disallowed C-extension or built-in module
    

    I'm not sure what that is, or why I get it only when trying to include user object on my return result, but for now I can just not include the user object to avoid the error.