Django db_index and unique=在OneTONE关系上为真

Django db_index and unique=在OneTONE关系上为真,django,django-sphinx,Django,Django Sphinx,我正在扩展django的用户模型 from django.contrib.auth.models import User class UserProfile(models.Model): user = models.OneToOneField(User, _(u"User")) 那么,是否可以在用户字段中使用db_index=True和unique=True?然后我需要实现用户名搜索。我想到了斯芬克斯。有什么想法吗?也许有一些好的教程链接?TIA按用户名搜索只会是: UserProf

我正在扩展django的用户模型

from django.contrib.auth.models import User

class UserProfile(models.Model):
    user = models.OneToOneField(User, _(u"User"))

那么,是否可以在用户字段中使用db_index=True和unique=True?然后我需要实现用户名搜索。我想到了斯芬克斯。有什么想法吗?也许有一些好的教程链接?TIA

按用户名搜索只会是:

UserProfile.objects.filter(user__username="dave")
另外,我不确定第二个参数应该做什么;宣言应该是这样的:

class UserProfile(models.Model):
    user = models.OneToOneField(User)
models.OneToOneField的所有额外参数都应为关键字参数


正如cathy指出的,models.OneToOneField意味着unique=True。IIRC,这意味着Django将自动为此字段创建索引。如果不需要索引,则需要显式设置db_index=False。

按用户名搜索只需:

UserProfile.objects.filter(user__username="dave")
另外,我不确定第二个参数应该做什么;宣言应该是这样的:

class UserProfile(models.Model):
    user = models.OneToOneField(User)
models.OneToOneField的所有额外参数都应为关键字参数


正如cathy指出的,models.OneToOneField意味着unique=True。IIRC,这意味着Django将自动为此字段创建索引。如果不需要索引,则需要显式设置db_index=False。

不必因为其OneTooneField而设置唯一,也不必因为其OnetoOneFieldThanx而设置唯一。这就是我想知道的。这就是我想知道的