Want to create referral program in django - django-models

I want to create referral program using django models, referral should be look like linkedIn connections "https://www.linkedin.com/help/linkedin/answer/a545636/your-network-and-degrees-of-connection?lang=en" like 1st degree and 2nd degree.
ex-
If A refer B and then B refer C then for A (B is 1st degree connection and C will be 2nd degree connection).
How can I implement this?
I wrote logic in models.py but not getting required results.
models.py
class SaturnReferral(models.Model):
SaturnUser = models.CharField(max_length=50)
SaturnEmail = models.CharField(max_length=50, default="null")
ReferralCode = models.CharField(max_length=7)
Upvote = models.PositiveIntegerField(default=0)
Downvote = models.PositiveIntegerField(default=0)
recommended_by = models.ForeignKey(User, on_delete=models.CASCADE, blank=True, null=True, related_name='ref_by')
def __unicode__(self):
return unicode(self.recommended_by.id)
def save(self, *args, **kwargs):
#first_degree_connections
main_user1=self.SaturnUser
if recommended_by:
referrer1 = self.recommended_by.USERNAME_FIELD
UserReferral.create.object(main_user=main_user1,first_degree_users=referrer1)
mydata2 = SaturnConnections.objects.filter(creator=main_user).values()
referred1 = mydata2.friend
for u in referred1:
UserReferral.create.object(main_user=main_user1,first_degree_users=u)
#second-degree connections
if recommended_by:
main_user1=self.recommended_by.USERNAME_FIELD
mydata2 = SaturnConnections.objects.filter(creator=main_user).values()
second_degree_from_referred = mydata2.friend
for u1 in second_degree_to_referred:
UserReferral1.create.object(main_user=self.SaturnUser,second_degree_users = u1)
for u in referred1:
main_user1=u
mydata2 = SaturnConnections.objects.filter(creator=main_user).values()
second_degree_to_referred = mydata2.friend
for u1 in second_degree_to_referred:
UserReferral1.create.object(main_user=self.SaturnUser,second_degree_users = u1)
super(SaturnReferral, self).save(*args, **kwargs)
#Saturn Connections
class UserReferral(models.Model): # store first-degree connections
main_user = models.CharField(max_length=50)
first_degree_users = models.CharField(max_length=50)
class UserReferral1(models.Model): # store second-degree connections
main_user = models.CharField(max_length=50)
second_degree_users = models.CharField(max_length=50)
class SaturnConnections(models.Model):
created = models.DateTimeField(auto_now_add=True, editable=False)
creator = models.CharField(max_length=15)
friend = models.CharField(max_length=15)

Related

An error occurs when tries to add a model in django-admin. (many-to-many relations)

I want to connect with Product model and productcategory model with manytomany relation. When I write it like this.
class Product(models.Model):
id = models.CharField(primary_key=True, default=generateUUID(), max_length=36, unique=True, editable=False)
category = models.ManyToManyField('ProductCategory')
creatorid = models.ForeignKey('User', models.DO_NOTHING, db_column='creatorId', related_name='creator') # Field name made lowercase.
createdat = models.DateTimeField(db_column='createdAt', default=timezone.now) # Field name made lowercase.
updatedat = models.DateTimeField(db_column='updatedAt') # Field name made lowercase.
status = models.TextField(choices=PRODUCT_STATUS.choices) # This field type is a guess.
class Meta:
managed = False
db_table = 'product'
class ProductCategory(models.Model):
id = models.CharField(primary_key=True, default=generateUUID(), max_length=36, unique=True, editable=False)
name = models.TextField()
rootcategory = models.TextField(choices=PRODUCT_ROOT_CATEGORY.choices, blank=True, null=True, db_column='rootCategory') # Field name made lowercase. This field type is a guess.
icon = models.TextField()
class Meta:
managed = False
db_table = 'product_category'
When I try to add a product model in admin, the following error occurs. What's the problem?
Error ScreenShot

Django model keep field unchanged during update

My model is:
class Customer(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
created_by = models.EmailField(max_length=254, blank=True)
first_name = models.CharField(max_length=100, blank=False)
last_name = models.CharField(max_length=100, blank=False)
email = models.EmailField(max_length=254, blank=True)
phone = models.CharField(max_length=14, blank=False)
I would like created_at and created_by to stay unchanged upon update of the object. created_at works that way because of auto_now_add=True but I can't find a similar option for other model fields. Any ideas?
A simple solution with serializers is to override the update() function and refrain from updating the created_at and created_by fields.
For example in, lets say, serilalizers.py:
class CustomerSerializer(serializers.Serializer):
first_name = serializers.CharField(max_length=100)
last_name = serializers.CharField(max_length=100)
email = serializers.EmailField(max_length=254)
phone = serializers.CharField(max_length=14)
# Other functions like create() ...
def update(self, instance, validated_data):
instance.first_name = validated_data.get('first_name', instance.first_name)
instance.last_name = validated_data.get('last_name', instance.last_name)
instance.email = validated_data.get('email', instance.email)
instance.phone = validated_data.get('phone', instance.phone)
instance.save()
return instance

Creating a Django model with unique values for each user

I would like to create a site that helps users to remember meaning of certain words.
class Word(models.Model):
word = models.CharField(max_length=30, unique=True)
meaning = models.CharField(max_length=200)
memory_strength = models.FloatField()
user = models.ForeignKey(User)
I want each user to have individual (unique) value of memory_strength for every item of Word, while values of word and meaning would be the same for each and every user. How can I achieve that?
class Word(models.Model):
word = models.CharField(max_length=30, unique=True)
meaning = models.CharField(max_length=200)
class Memory(models.Model):
memory_strength = models.FloatField()
user = models.ForeignKey(User)
word = models.ForeignKey(Word)

Django Model with math

Hey i have a problem with my django model.
i have one class like this:
class Test(model.Models):
EV_Nr = models.IntegerField(unique=True)
Factor = models.DecimalField(max_digits=5, decimal_places=2, blank=False)
class Polter(models.Model):
EV = models.ForeignKey(Test)
RM = models.DecimalField(max_digits=10, decimal_places=2, default=0)
FM = models.DecimalField(max_digits=10, decimal_places=2, default=0)
My Problem:
RM or FM has to be 0, if both >0, it's an error and can not be saved.
but for example: RM = 10 and FM = 0. It's ok.
-> and the model has to check for the Factor in my Test Model and to do the
following: FM = RM / Faktor(from Test Model) and save it in my databese.
How can i do this. After two days of searching I have no idea.
Thanks for your help

Retrieve data from two tables with many to many relationship in Django?

I want to retrieve a data which has user id 1 from the table called file. The user and file table and many to many relationship. I want to show the file related to that user.
class GalaxyUser(models.Model):
id = models.IntegerField(primary_key=True)
create_time = models.DateTimeField(null=True, blank=True)
update_time = models.DateTimeField(null=True, blank=True)
email = models.CharField(max_length=765)
password = models.CharField(max_length=120)
external = models.IntegerField(null=True, blank=True)
deleted = models.IntegerField(null=True, blank=True)
purged = models.IntegerField(null=True, blank=True)
username = models.CharField(max_length=765, blank=True)
form_values_id = models.IntegerField(null=True, blank=True)
disk_usage = models.DecimalField(null=True, max_digits=16, decimal_places=0, blank=True)
class Meta:
db_table = u'galaxy_user'
class File(models.Model):
users = models.ManyToManyField(GalaxyUser)
file_name = models.CharField(max_length=100)
type = models.CharField(max_length=10)
source = models.CharField(max_length=100)
start_date = models.TextField()
end_date = models.TextField()
duration = models.TextField()
size = models.TextField()
flag = models.TextField()
If the user is logged in as user, I get the user_id let's say it's 1. Now, I want to retrieve the file name related to user 1 only. How can I do that?
File.objects.filter(users=GalaxyUser.objects.get(id=1))
You can use File.objects.filter(users__id__exact=1). Refer here.
user = GalaxyUser.objects.get(id=1)
files = user.file_set.all()
See more about ManyToMany lookups here

Resources