thank Melvyn :) i understands right now!

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

> On Monday 20 February 2017 08:39:07 carlos wrote:
>
> > 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?
>
>
>
> Sure. Whatever "the best" may be.
>
>
>
> Best = "Make sure we always get the same category":
>
>
>
> class CategoryInfo(models.Model):
>
> my_model = models.ForeignKey('MyModel', on_delete=models.CASCADE,
> releated_name='+')
>
> category = models.ForeignKey(Category, on_delete=models.CASCADE,
> related_name='+')
>
> is_primary = models.BooleanField(default=False)
>
>
>
> class MyModel(models.Model):
>
> post = models.CharField(....)
>
> slug = models.SlugField(.....)
>
> categories = models.ManyToMany(Category, through=CategoryInfo)
>
>
>
> def get_absolute_url(self):
>
> try:
>
> category = CategoryInfo.objects.get(my_model=self, is_primary=True)
>
> except Category.DoesNotExist:
>
> category = CategoryInfo.objects.filter(
>
> my_model=self
>
> ).first()
>
> return "/%s/%s-%s/" % (category.slug, self.id, self.slug)
>
>
>
> Best = "Don't depend on URL structure":
>
>
>
> class MyModel(models.Model):
>
> # .... fields ....
>
>
>
> def get_absolute_url(self):
>
> category = self.categories.first()
>
> kwargs = {
>
> 'category_slug': category.slug,
>
> 'pk': self.pk,
>
> 'slug': self.slug,
>
> }
>
> return reverse('mymodel_detail', args=(), kwargs=kwargs)
>
>
>
> urls.py:
>
> urlpatterns = (
>
> url(
>
> '^(?P<category_slug>[a-z0-9-A-Z_-]+)/(?<pk>[0-9]+)-(?P<slug>
> [a-z0-9-A-Z_-]+)/$',
>
> views.MyModelDetailView.as_view(),
>
> name='mymodel_detail'
>
> ),
>
> )
>
>
>
> --
>
> 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/4115182.X1rUVAgno4%40devstation
> <https://groups.google.com/d/msgid/django-users/4115182.X1rUVAgno4%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-7rO1OLrweaVWr%3D3CEkuwtb7SXQhr3oBDUPtAQ6%2Bt1bBfX6A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to