I'm looking to create a url to visit a user profile using a slug
. Ie. I want to visit /profile/myname
to bring up my user profile. What I am having difficulties with is implementing the slug
field for a user model.
Additionally, not sure if this matters or not but I have created a UserProfile
model which extends the standard User
model as shown below:
from django.contrib.auth.models import User
from autoslug import AutoSlugField
class UserProfile(models.Model):
user = models.ForeignKey(User, related_name='profile_name')
about_me = models.TextField(null=True, blank=True)
slug = AutoSlugField(populate_from='User.username', default='', unique=True)
which gives the error (when trying to migrate):
DETAIL: Key (slug)=() is duplicated.
I believe the urls to be correct but have included it for reference (in an app named profile):
url(r'^(?P<slug>[\w-]+)', views.detail, name='detail')
It means you already have some records in userprofile
table with empty/null values in slug
field. Because you've marked that field as unique=True
it can only have one field with empty value. To avoid this error, delete the records with empty value in slug field, or just assign them a unique slug and you'll be good to go.
And as you can understand from above, having default=''
in a field that has unique=True
wont work. Unique means unique, even ''
as an empty value is considered a unique value and can be used in just one row if you have unique=True
. That also means you cannot have any default value in a unique field.