Я написал простой код для расчета процента заполнения профиля. То, как я это сделал, не охватывает поле, являющееся внешним ключом для модели профиля. Вот как я это сделал
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
Это основной, но как мне справиться с fk связанной частью? У кого-нибудь есть лучший алгоритм с учетом полей Fk?