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,870 points
2,396 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,770 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,840 points
10,603 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,840 points
60,887 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,090 points

edited Dec 14, 2018 by Anirudh 1,463 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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
+5 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,790 points
4,070 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,770 points
2,388 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,770 points
5,038 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP