开发者

django lambda and django-activity-stream

I am not familiar with the lambda function itself, and don't really know how to debug this issue.

Django-1.1.2

I am using, django-activity-stream in order to render activity streams for my 开发者_如何学JAVAusers. In the documentation for this items it says that you need to pass two lambda functions to incorporate with existing newtworks, such as django-friends(the one I am using)

Here are the functions that need to be pasted into your settings.py file.

ACTIVITY_GET_PEOPLE_I_FOLLOW = lambda user: get_people_i_follow(user)
ACTIVITY_GET_MY_FOLLOWERS = lambda user: get_my_followers(user)

I have done so, but now everytime I try and render the page that makes use of this, I get the following traceback.

Caught NameError while rendering: global name 'get_people_i_follow' is not defined

Although this has been set in my settings...

Your help is much appreciated!


Somewhere above where these lambdas are defined, you need to import the names get_people_i_follow and get_my_followers. I'm not familiar with django-activity-stream, but it's probably something like from activity_stream import get_people_i_follow, get_my_follwers.

Lambda is a keyword for creating anonymous functions on the fly, so the meaning of your code is basically the same as if you had written the following.

def ACTIVITY_GET_PEOPLE_I_FOLLOW(user):
    return get_people_i_follow(user)

def ACTIVITY_GET_MY_FOLLOWERS(user):
    return get_my_followers(user)


You need to make sure that the functions get_people_i_follow and get_my_followers are imported into your settings files.

e.g.:


from activity_stream.models import get_people_i_follow, get_my_followers

Lambda is just a shorthand for defining a function so:


ACTIVITY_GET_PEOPLE_I_FOLLOW = lambda user: get_people_i_follow(user)

Is equivalent to:


def activity_get_people_i_follow(user):
    return get_people_i_follow(user)
ACTIVITY_GET_PEOPLE_I_FOLLOW = activity_get_people_i_follow

Which upon reflection means you don't gain a lot in this case. However if you needed to avoid importing those function too early in your settings file (i.e. due to circular import) then you could do:


def activity_get_people_i_follow(user):
    from activity_stream.models import get_people_i_follow
    return get_people_i_follow(user)
ACTIVITY_GET_PEOPLE_I_FOLLOW = activity_get_people_i_follow

and just import the activity stream function as you need it.

UPDATE: looks like defining these settings is a red-herring:

https://github.com/philippWassibauer/django-activity-stream/blob/master/activity_stream/models.py#L133

As you can see these settings are only needed if you are not using the default activity streams. So simply remove them from your settings file.

The seg-fault is probably due to an infinite recursion occurring, as get_people_i_follow calls whatever function is defined by ACTIVITY_GET_PEOPLE_I_FOLLOW, which in this case calls get_people_i_follow again...


If you are integrating with a pre-existing network I don't believe you're actually supposed to write verbatim:

ACTIVITY_GET_PEOPLE_I_FOLLOW = lambda user: get_people_i_follow(user)
ACTIVITY_GET_MY_FOLLOWERS = lambda user: get_my_followers(user)

I believe the author was just showing an example that ACTIVITY_GET_PEOPLE_I_FOLLOW and ACTIVITY_GET_MY_FOLLOWERS need to be set to a lambda or function that accepts one user argument and returns a list of users. You should probably be looking for something like friends_for_user in django-friends, or writing your own functions to implement this functionality.

get_people_i_follow is indeed defined in activity_stream.models but it is just importing what's defined in settings.py. So if settings.py has ACTIVITY_GET_PEOPLE_I_FOLLOW = lambda user: get_people_i_follow(user) you're going to get a wild and crazy circular import / infinite recursion.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜