How do I build a website with referral link on Django

0 votes
How do I build a website with referral link on Django, every user will have a link that has their name.
Nov 12, 2020 in Python by Rajiv
• 8,880 points
358 views

1 answer to this question.

0 votes
simple solution..
import hashlib

Refferal(Model):
    user = OneToOne(User)
    upline=ForeignKey(User, null=True, related_name='downlines')
    ref_code = CharField()

def gen_code(self):
    text = '%s' % self.id
    code = hashlib.md5(text.encode())
    return code.digest()

def save(self, *arg, kwargs):
     if self._state.adding:
          self.ref_code = self.gen_code()
     return super().save(*args, kwargs)

Use
try :
    upline = Referral.objects.get(ref_code=some_code)
except:
    upline= None

user_refferal = Referral(user=user, upline=upline)
user_refferal.save()

Hope this helps!!!

answered Nov 12, 2020 by Gitika
• 65,870 points

Related Questions In Python

0 votes
1 answer
0 votes
1 answer

How do I add a link from the Django admin page of one object to the admin page of a related object?

Hello @kartik, Set show_change_link to True (False by default) in your inline ...READ MORE

answered Jun 12, 2020 in Python by Niroj
• 82,720 points
2,788 views
0 votes
1 answer

How do I call a Django function on button click?

Hello @kartik, There are 2 possible solutions that ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,720 points
17,473 views
0 votes
1 answer

How do I generate some random numbers with a distribution using Python?

scipy.stats.rv_discrete is what you ned IMHO. You can supply ...READ MORE

answered Oct 31, 2018 in Python by Anirudh
• 2,080 points

edited Dec 14, 2018 by Anirudh 278 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 2,058 views
0 votes
0 answers
+4 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,810 points
1,570 views
0 votes
1 answer

I want to download a file from the website by web scraping. Can anyone explain how to do this in jupyter lab (python) with an example?

Hey, Web scraping is a technique to automatically ...READ MORE

answered Apr 7, 2020 in Python by Gitika
• 65,870 points
670 views
0 votes
1 answer

How do I install a Python package with a .whl file?

I just used the following which was ...READ MORE

answered Nov 20, 2020 in Python by Gitika
• 65,870 points
1,420 views