I have written a simple code for calculating the percentage of profile complete. The way i have done does not covers the field which are a foreign key to the profile model. Here is how i have done
def calculate_profile_percentage(self, context):
# fk related field are not here
fields = {'full_name': 10, 'age': 10, 'city': 10, 'address': 10}
completed_profile_percent = 0
try:
profile_instance = model_to_dict(Profile.objects.get(user_id=context.get('id')))
print('profile get_instance', profile_instance)
for field in profile_instance:
print ("field", field)
if fields.get(field) is not None:
print("field found", field)
completed_profile_percent += fields[field]
except Profile.DoesNotExist:
logger.error("Profile does not exist")
print("completed_profile_percent", completed_profile_percent)
return completed_profile_percent
This is a basic one but how do i handle fk related part either. Does anyone have better algorithm considering the Fk fields either?