我写了一个简单的代码来计算配置文件完成百分比。我所做的方式并未涵盖作为概要文件模型的外键的字段。这是我做的事
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字段有更好的算法?