IntegrityError when rendering and won't take the Autocomplete value

Go To StackoverFlow.com

-1

Explanation: The template renders the autocomplete perfectly... But once I test it and click "add manager" it returns this error:

Exception Type:     IntegrityError
Exception Value:    investments_managesfund.manager_id may not be NULL

I can't figure this out. Because in the POST, it's sending the csrf_token and manager username.

Add manger to fund VIEWs.py:

@login_required
    def add_manager_to_fund(request, fund_id):
        fund = get_object_or_404(Fund, id=fund_id)
        if request.POST['from'] == 'fund':
            if 'omnibox' in request.POST:
                manager = ManagerProfile.objects.get(user=request.user)
                if not ManagesFund.objects.filter(verified=True, fund=fund, manager=manager).exists() and not request.user.is_staff:
                    return HttpResponseRedirect('/profile/fund/' + fund_id + '/')       
                try:
                    mp = ManagerProfile.objects.get(user__username=request.POST['omnibox'])
                    fundManager = ManagesFund.objects.get(manager=mp, fund=fund, from_user=True)
                    if not fundManager.verified:
                        fundManager.verified = True
                        fundManager.save()
                except ObjectDoesNotExist:
                    fundManager, created = ManagesFund.objects.get_or_create(manager__user__username=request.POST['omnibox'], fund=fund, from_user=False)
                    if created:
                        fundManager.save()
        return HttpResponseRedirect('/profile/edit/fund/' + fund_id + '/')

autocomplete VIEWs.py:

@login_required
def managers(request):
    managers = ManagerProfile.objects.exclude(user=None)
    result = {}
    for manager in managers:
        result[manager.user.get_full_name()] = manager.user.username
    return HttpResponse(json.dumps(result))

The autocomplete view passes the username and add_manager_to_fund() takes a username. How do I fix this?

2012-04-04 21:13
by Modelesq


0

You really should restructure your code. You have two calls in the try that could be raising ObjectDoesNotExist. Which I guess one of them must be, but we can't tell which. That aside, it appears your are creating a ManagesFund without specifying a manager. That is what is causing your IntegrityError.

If you need to look up the manager in from username (which is looks like you are trying to). Do it ahead of time for get_or_create.

2012-04-04 21:40
by John
Ads