Hi, Melvyn yes i use similar but for the question is
the best way?

def get_absolute_url(self):

return '/%s/%s-%s/' % (self.categories.all()[0].slug, self.id, self.slug)
#this way a take first category, but other best way to do that?


thank for helps :)


On Mon, Feb 20, 2017 at 5:06 AM, Melvyn Sopacua <m.r.sopa...@gmail.com>
wrote:

> On Sunday 19 February 2017 23:00:54 carlos wrote:
>
>
>
> > class Category(...):
>
> > name = CharField(....)
>
> > slug = SlugField(.....)
>
> >
>
> > class MyModel(modesl.Model):
>
> > post = models.CharField(....)
>
> > slug = models.SlugField(.....)
>
> > categories = models.ManyToMany(Category)
>
> >
>
> > def get_absolute_url(self):
>
> > return "/%s/%s-%s/" % (self.categories.slug, self.id,
>
> > self.slug)
>
>
>
> Since this is many, this will never work, since you can have more then
> one category. You have to decide which category is the preferred one or
> just use the first one that comes up.
>
>
>
> > #error 'ManyRelatedManager' object has no attribute 'slug'
>
>
>
> Because the related manager is not a model. With a foreign key (many to
> one) the remote side is always one, so Django links the ForeignKey with a
> model instance.
>
> With a ManyToMany relationship, the remote side can be more than one, so
> the RelatedManager is a ModelManager (for all intents and purposes a
> QuerySet).
>
>
>
> The quick and dirty way:
>
> def get_absolute_url(self):
>
> return '/%s/%s-%s/' % (self.categories.first().slug, self.id, self.slug)
>
>
>
> (Of course, this still is probably incorrect, since this doesn't contain
> the app prefix).
>
>
>
> --
>
> Melvyn Sopacua
>
> --
> 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 django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> 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/9021467.5BfZDu2IXj%40devstation
> <https://groups.google.com/d/msgid/django-users/9021467.5BfZDu2IXj%40devstation?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>



-- 
att.
Carlos Rocha

-- 
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 django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
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/CAM-7rO2KEyY0npkUYo1wbaSu18dUovLbqEW%3D_tbEkH0eSu7s9g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to