def global_context(request): print "adding context %s" % request print "user: %s, authenticated? %s" % (request.user, request.user.is_authenticated()) if request.user.is_authenticated(): return { "groups": WishlistGroup.get_groups_by_user(request.user) } return {}
def __init__(self, *args, **kwargs): # Only in case we build the form from an instance # (otherwise, 'groups' list should be empty) if 'instance' in kwargs: # We get the 'initial' keyword argument or initialize it # as a dict if it didn't exist. initial = kwargs.setdefault('initial', {}) # The widget for a ModelMultipleChoiceField expects # a list of primary key for the selected data. initial['groups'] = [grp.pk for grp in kwargs['instance'].wishlistgroup_set.all()] if 'group' in kwargs: initial = kwargs.setdefault('initial', {}) groups = initial.setdefault('groups', []) groups.append(kwargs['group'].pk) del kwargs['group'] user = kwargs.pop('user') ModelForm.__init__(self, *args, **kwargs) self.fields['groups'].queryset = WishlistGroup.get_groups_by_user(user=user)