2011-10-31 74 views
0

我能够返回只有CharFields/Dates/Integers的django模型,但现在我试图返回具有ForeignKey属性的模型,并且出现此错误Flex中我NetStatusEvent.NET_STATUS onerror事件处理程序:当Pyamf返回Django对象时发生500错误Flex

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

下面是一些重要的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 

然后,当我想通过检索这些PyAMF的对象,这是我正在使用的方法,它给了我上面列出的错误:

@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 

有趣的是,如果我改变我的get_challenge_act方法返回ChallengeAct对象的属性,我没有得到这个错误。因此,我可以返回属于ChallengeAct的属性或对象,但不返回ChallengeAct本身。例如,下面的代码返回一个挑战对象没有任何错误:

return c.challenge 

所以它似乎有一些问题与回归模型foreginkey一些属性的Django模型?难道我做错了什么?

回答

0

通过淘汰过程,我发现它是ChallengeAct上的用户对象导致了问题,并且在保存之后和返回之前通过将用户对象设置为None来消除模糊500错误。

@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 

我很想听听为什么会发生这种情况。有人有主意吗?

--update--我发现,通过查看终端日志,运行我的runserver命令后,我可以看到实际的500错误是什么。所以实际的错误是:

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

我不知道那是什么,或者为什么我在尝试,包括我的返回结果的用户对象仅当,但现在我可以不包括用户对象以避免错误。

相关问题