我有三种模式:
class UserProfile:
user_id = OneToOneField(User, related_name='profile')
name = CharField
class User:
# standard django model
class Channel:
owner = ForeignKey(User)
现在,我希望对用户名进行频道过滤。所以我能做的是:
Channel.objects.filter(owner__profile__name__icontains='foo')
但是这会加入User表然后加入UserProfile,这不是最好的,因为我想在user_id上加入UserProfile表(我会有一个连接而不是两个)
我尝试将另一个外键添加到这样的模型中:
class Channel:
owner = models.ForeignKey(
User,
db_column='owner_id',
)
owner_profile = models.ForeignKey(
UserProfile,
db_column='owner_id',
to_field='user_id')
但是Django不喜欢它......
posts.Post: (models.E007) Field 'owner_profile' has column name 'owner_id' that is used by another field.
HINT: Specify a 'db_column' for the field.
这有什么干净的解决方法吗?