djangodjango-modelsdjango-viewsreferrals

Referral application in Django


I want to create a Django application referral system for profiles created by user upon login

whenever a people login they get a option to create a profile and in that create profile form their is input for referral code which eventually add some point to referred by person

here is my Profile Model

class Profile(models.Model):
  owner = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE,default='')
  name = models.CharField(max_length=200,null=True,blank=True)
  number = models.CharField(max_length=11,default=None,null=True)
  text = models.TextField(max_length=520)
  facebook = models.URLField(name='facebook',default=None,null=True)
  linkedin = models.URLField(name='linkedin',default=None,null=True)
  instagram = models.URLField(name='instagram',default=None,null=True)
  profile_picture = models.ImageField(upload_to='profile_pic/', max_length=None)
  published_date = models.DateTimeField(auto_now_add=True, null=True)
  updated_date = models.DateTimeField(blank=True, null=True)
  is_active = models.BooleanField(default=False)

 def __str__(self):
    return self.name

what should be added to this profile for referral code ??

and how to create a referral class ???


Solution

  • You could add a field like this to Profile:

    referral_code = models.CharField(max_length=20, unique=True)
    

    This would have to be populated with some random string on Profile create.

    You can then to the following when creating a new user.

    try:
        referral_profile = Profile.objects.get(referral_code=ref_code)
    except Profile.DoesNotExist:
        pass
    
    if referral_profile:
        # do something here. Use 'referral_profile' to get user that referred this user.
        pass