0

I have something like this in views.py.

    if form.is_valid():

        ref_user = User.objects.get(
        username=form.cleaned_data['referrer'])

        if User.objects.filter(username=ref_user).exists():

            user = User.objects.create_user(
            username=form.cleaned_data['username']
            )   

            direct = DirectReferral.objects.create(
                name = user,
                referrer = ref_user # New created referrer
            )

            if DirectReferral.objects.filter(referrer=ref_user).exists():

                # Get the referrer of the newly created referrer in DirectReferral database

                indirect = IndirectReferral.objects.create(
                    name = user,
                    referrer = # This should be the referrer of the newly created referrer
                )

How do I make a query or get the referrer of the newly created referrer?

To further understand what I mean please check image below.
enter image description here

I hope you understand my question.

4

1 回答 1

1

我猜代码中有一些错误。

如果 form.is_valid():

    ref_user = User.objects.get(
    username=form.cleaned_data['referrer'])

    if User.objects.filter(username=ref_user).exists():

        user = User.objects.create_user(
        username=form.cleaned_data['username']
        )   

        direct = DirectReferral.objects.create(
            name = user,
            referrer = ref_user # New created referrer
        )

        if DirectReferral.objects.filter(referrer=ref_user).exists():

            # Here you are checking for something you just created. 
            # I guess this should be 
            # DirectReferral.objects.filter(name=ref_user).exists()

            # Get the referrer of the newly created referrer in DirectReferral database

            indirect = IndirectReferral.objects.create(
                name = user,
                referrer = # This should be the referrer of the newly created referrer
            )

如果您打算找到当前创建的用户的推荐用户的推荐人,您可以这样做。

如果 form.is_valid():

    ref_user = User.objects.get(
    username=form.cleaned_data['referrer'])

    if User.objects.filter(username=ref_user).exists():

        user = User.objects.create_user(
        username=form.cleaned_data['username']
        )   

        direct = DirectReferral.objects.create(
            name = user,
            referrer = ref_user # New created referrer
        )

        try:
            referral_of_ref_user = DirectReferral.objects.get(name=ref_user).referrer
        except DirectReferral.DoesNotExist:
            # Do what needs to be done if direct referral not exist.
            print "No referrer"
            # Get the referrer of the newly created referrer in DirectReferral database
        else:
            indirect = IndirectReferral.objects.create(
                name = user,
                referrer = referral_of_ref_user
            )

编辑:

通过对您的代码进行最少的编辑:

如果 form.is_valid():

    ref_user = User.objects.get(
    username=form.cleaned_data['referrer'])

    if User.objects.filter(username=ref_user).exists():

        user = User.objects.create_user(
        username=form.cleaned_data['username']
        )   

        direct = DirectReferral.objects.create(
            name = user,
            referrer = ref_user # New created referrer
        )

        if DirectReferral.objects.filter(name=ref_user).exists():

            # Get the referrer of the newly created referrer in DirectReferral database

            indirect = IndirectReferral.objects.create(
                name = user,
                referrer = DirectReferral.objects.filter(name=ref_user)[0].referrer
            )
于 2016-04-19T11:17:02.400 回答