oleg.foreigner
oleg.foreigner

Reputation: 1023

db_index and unique=True on OneToOne relationship

I'm extending django's User model.

from django.contrib.auth.models import User

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

so, is it possible to use db_index=True and unique=True with user field? and then I need to implement search by username. I thought about Sphinx. Any thoughts on this? maybe some links with good tutorials? TIA

Upvotes: 1

Views: 1159

Answers (1)

Mike DeSimone
Mike DeSimone

Reputation: 42795

Search by username would just be:

UserProfile.objects.filter(user__username="dave")

Also, I'm not sure what your second parameter is supposed to do; the declaration should look like:

class UserProfile(models.Model):
    user = models.OneToOneField(User)

All extra arguments to models.OneToOneField should be keyword arguments.

As cathy pointed out, models.OneToOneField implies unique=True. IIRC, this means Django will automatically create an index for this field. (You would need to explicitly set db_index=False if you didn't want the index.)

Upvotes: 6

Related Questions