Newtwork Marketing System

23 views
Skip to first unread message

John Ayodele

unread,
Jun 15, 2023, 8:16:21 AM6/15/23
to django...@googlegroups.com
Hi Everyone!
So I want to know how to implement a binary tree to a web app.
This is how the app works:

—  Person A opens an account
—  Person A refers to people
— Person B invests,
— Person A get 10% of Person B's investment 
So it goes on iteratively.

Please, how can I implement this in Django?

 I've never done something like this before, so it's new to me.

Help me accomplish this task by assisting me in any way you can. :) 

Abdul Felix

unread,
Jun 15, 2023, 8:37:25 AM6/15/23
to django...@googlegroups.com

# views.py

from django.shortcuts import render, redirect
from .models import Person, BinaryTree

def create_person(request):
    if request.method == 'POST':
        name = request.POST.get('name')
        account = float(request.POST.get('account'))
        referral_id = request.POST.get('referral')

        referral = None
        if referral_id:
            referral = Person.objects.get(id=referral_id)

        person = Person.objects.create(name=name, account=account, referral=referral)

        # Update binary tree structure
        if referral:
            binary_tree = BinaryTree.objects.get(parent=referral)
            if not binary_tree.left_child:
                binary_tree.left_child = person
            elif not binary_tree.right_child:
                binary_tree.right_child = person
            binary_tree.save()

            # Calculate and update referral bonus
            referral_bonus = account * 0.1
            referral.account += referral_bonus
            referral.save()

        return redirect('person_detail', person_id=person.id)
    else:
        persons = Person.objects.all()
        return render(request, 'create_person.html', {'persons': persons})




# models.py

from django.db import models

class Person(models.Model):
    name = models.CharField(max_length=100)
    account = models.DecimalField(max_digits=10, decimal_places=2)
    referral = models.ForeignKey('self', on_delete=models.SET_NULL, null=True, blank=True)

class BinaryTree(models.Model):
    parent = models.ForeignKey(Person, on_delete=models.CASCADE, related_name='binary_tree_parent')
    left_child = models.ForeignKey(Person, on_delete=models.CASCADE, related_name='binary_tree_left_child', null=True, blank=True)
    right_child = models.ForeignKey(Person, on_delete=models.CASCADE, related_name='binary_tree_right_child', null=True, blank=True)



Create_person.html


<!DOCTYPE html>
<html>
<head>
    <title>Create Person</title>
</head>
<body>
    <h1>Create Person</h1>
    <form method="post">
        {% csrf_token %}
        <label for="name">Name:</label>
        <input type="text" name="name" required><br><br>

        <label for="account">Account Balance:</label>
        <input type="number" name="account" step="0.01" required><br><br>

        <label for="referral">Referral:</label>
        <select name="referral">
            <option value="">None</option>
            {% for person in persons %}
                <option value="{{ person.id }}">{{ person.name }}</option>
            {% endfor %}
        </select><br><br>

        <input type="submit" value="Create">
    </form>
</body>
</html>





Or follow me @ GitHub.com/developer-felix I post more codes on federals

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAP7pJ3j3942AExQzHwvpxO1c0Zgspw4NCvHqaw6CcgzEfQeRLw%40mail.gmail.com.

Benjamin Schollnick

unread,
Jun 15, 2023, 9:37:03 AM6/15/23
to django...@googlegroups.com
Hi Everyone!
So I want to know how to implement a binary tree to a web app.
This is how the app works:

—  Person A opens an account
—  Person A refers to people
— Person B invests,
— Person A get 10% of Person B's investment 
So it goes on iteratively.

Please note, nothing here in your description needs (or begs) to be a binary tree.

Have a referred field in the person table, which points to the PK of the person that referred them.

Then you just do a filter on that persons referred field PK to find out who referred them.

Yes, a binary tree is suppose to be one of the faster searches for certain types of data…But as described I’m not sure it’s a good fit.

- Benjamin


Dean Mahori

unread,
Jun 20, 2023, 1:56:46 PM6/20/23
to django...@googlegroups.com
Hi anyone to teach me forex trading guys...


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Dilmurod Dilmurod

unread,
Jun 27, 2023, 12:05:55 PM6/27/23
to django...@googlegroups.com

Pay, 15-iyn, 2023, 13:16 John Ayodele <ayodel...@gmail.com>:
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Dilmurod Dilmurod

unread,
Jun 27, 2023, 12:06:18 PM6/27/23
to django...@googlegroups.com

Pay, 15-iyn, 2023, 14:37 Benjamin Schollnick <bscho...@schollnick.net>:
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Dilmurod Dilmurod

unread,
Jun 27, 2023, 12:06:22 PM6/27/23
to django...@googlegroups.com

Dilmurod Dilmurod

unread,
Jun 27, 2023, 12:06:47 PM6/27/23
to django...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages