@classmethod
def get_obj_by_primary_value(cls, primary_value=None):
primary_key = None
fields = cls._meta.get_fields()
for obj in fields:
if obj.primary_key:
primary_key = obj.name
param_dic = {
primary_key: primary_value
}
data = cls.objects.filter(**param_dic).first()
return data
需要解决两点
1.找到主键属性
2.如果重写过滤条件
标签:obj,fields,primary,value,DJango,key,cls From: https://www.cnblogs.com/wusenwusen/p/17141910.html