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!!!