Extended user model to add Profile class want to slugify first_name last_name in profile class

182 views
Skip to first unread message

David

unread,
Jan 25, 2017, 6:50:12 AM1/25/17
to Django users
Hi

I have extended Django's default user model to add a Profile model; bio etc.

I want to slugify the users first_name last_name in profile class, so I can search for all X entities written by a specific user in the url form:

/blog/authors/bob-smith

When I try to add the slug field to my profile model that works fine. When I modify my admin to include the slug field, I cannot get the prepopulated_form to work:

class ProfileInline(admin.StackedInline):
  model = Profile
  can_delete = False
  verbose_name_plural = 'profile'
  fk_name = 'user'
  prepopulated_fields = {"slug": ('profile__first_name', 'profile__last_name')}

The profile is a onetoone to my user model.

Any ideas?

Thank you

David

unread,
Jan 25, 2017, 6:54:07 AM1/25/17
to Django users
<class 'accounts.admin.ProfileInline'>: (admin.E030) The value of 'prepopulated_fields["slug"][0]' refers to 'profile__first_name', which is not an attribute of 'accounts.Profile'.
<class 'accounts.admin.ProfileInline'>: (admin.E030) The value of 'prepopulated_fields["slug"][1]' refers to 'profile__last_name', which is not an attribute of 'accounts.Profile'.

Melvyn Sopacua

unread,
Jan 25, 2017, 10:08:45 AM1/25/17
to django...@googlegroups.com

On Wednesday 25 January 2017 03:50:12 David wrote:

 

> I have extended Django's default user model to add a Profile model;

> bio etc.

>

> I want to slugify the users first_name last_name in profile class, so

> I can search for all X entities written by a specific user in the url

> form:

>

> /blog/authors/bob-smith

 

Strike the idea to give admin control over the slug. Instead, use django-autoslug and make the field not editable. Let autoslug handle conflicts by using sane values for populate_from and unique_with.

 

Example (from the docs):

# autoslugify value from a custom callable

# (ex. usage: user profile models)
slug = AutoSlugField(populate_from=lambda instance: instance.user.get_full_name())

Which is basically what you try to accomplish in the admin.

--

Melvyn Sopacua

David

unread,
Jan 25, 2017, 10:43:51 AM1/25/17
to Django users
Thank you. That's very helpful. Now just need to work out how to make it a mandatory field in django admin, such that it is always created when a user is.

Melvyn Sopacua

unread,
Jan 25, 2017, 11:07:39 AM1/25/17
to django...@googlegroups.com

That can be done:

These profile models are not special in any way - they are just Django models that happen to have a one-to-one link with a user model. As such, they aren’t auto created when a user is created, but a django.db.models.signals.post_save could be used to create or update related models as appropriate.

 

Here's a mixin I use (lots of models have a unique 'name' attribute in this project) which also defines a handy permalink. As you can see, the field is not even editable (so won't show in the admin), yet for each saved model a slug is generated as the field is required (blank is not set and defaults to false).

 

class SlugMixin(models.Model):
slug = AutoSlugField(
populate_from='name', unique=True, editable=False,
)
_detail_view_name = None

@models.permalink
def get_absolute_url(self):
model_name = self._meta.object_name.lower()
viewname = self._detail_view_name or '{}_detail'.format(model_name)
return viewname, [str(self.slug)]
absolute_url = property(get_absolute_url, doc="Permalink")

class Meta:
abstract = True

--

Melvyn Sopacua

David

unread,
Jan 25, 2017, 11:22:16 AM1/25/17
to Django users
Thank you Melvyn. You've been very helpful!
Reply all
Reply to author
Forward
0 new messages