在我的应用程序中,同时有0个或1个配置文件的用户。 随着时间的推移,我的项目有许多不同的配置文件
从profileX访问用户很容易:profile_x_object.user
但是相反的关系呢?
我想找到最好的通用方法来创建从用户到其配置文件的关系
现在,我创建了一个名为profile
的属性来满足这个目的。
它的工作,但它需要为每一个新的配置文件,我添加了一段时间更新。
有没有更好的办法
以下是我的代码:
class User:
# ...
@property
def profile(self):
if hasattr(self, 'profilea'):
return self.profilea
if hasattr(self, 'probileb'):
return self.probileb
class BaseProfile(models.Model):
class Meta:
abstract = True
user = models.OneToOneField(settings.AUTH_USER_MODEL)
class ProfileA(BaseProfile, models.Model):
# ...
class ProfileB(BaseProfile, models.Model):
# ...
您可以使用model meta api并检查其相关模型为
BaseProfile
子类的1对1字段:相关问题 更多 >
编程相关推荐