On Wednesday 25 January 2017 07:43:50 David wrote:
> 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.
That can be done[1]:
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[2] 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
--------
[1]
https://docs.djangoproject.com/en/1.10/topics/auth/customizing/#extending-the-existing-user-model
[2]
https://docs.djangoproject.com/en/1.10/ref/signals/#django.db.models.signals.post_sa
ve
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/1862655.OKunrsylFK%40devstation.
For more options, visit https://groups.google.com/d/optout.