Sorry, I might have mixed things a bit.
You can use "link in category.link_set" or "category == link.category".

link_set is set by django when you declared the foreignkey

2016-08-21 19:42 GMT+02:00 Yunus <[email protected]>:
> Hm. I get it but it's look like there is no attribute category_set in link.
> Given me this error: 'Link' object has no attribute 'category_set'
>
> Here is my models:
>
> #category/models.py
> class Category(models.Model):
>  name = models.CharField("Name", max_length=100)
>  user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True)
>  time = models.DateTimeField(auto_now_add=True)
>  slug = models.SlugField(max_length=100)
>  objects = models.Manager()
>
>
>  def __str__(self):
>  return self.name
>
>
>  def get_absolute_url(self):
>  return reverse('category:category_detail', kwargs={"name": self.name,})
>
>
> #links/models.py
> class Link(models.Model):
>     title = models.CharField("Headline", max_length=100)
>     submitter = models.ForeignKey(settings.AUTH_USER_MODEL, null=True)
>     submitted_on = models.DateTimeField(auto_now_add=True)
>     rank_score = models.FloatField(default=0.0)
>     url = models.URLField("URL", max_length=250)
>     category = models.ForeignKey(Category, null=True)
>     description = models.TextField(blank=True)
>     slug = models.SlugField(unique=True)
>     img = models.ImageField(upload_to="thumbnail/", blank=True)
>     image_og = models.ImageField(upload_to="thumbnail/", blank=True,
> default="/static/images/entry/default.png")
>     with_votes = LinkVoteCountManager()
>     objects = models.Manager()
>
>
>     def __str__(self):
>         return self.title
>
>
>     def get_absolute_url(self):
>         return reverse('link:link_detail', kwargs={"category":
> self.category.slug, "slug": self.slug,
>                                                    'pk': str(self.id)
>                                                     })
>     ....
>
>
> On Sunday, August 21, 2016 at 2:56:57 PM UTC+3, ludovic coues wrote:
>>
>> You need to check somewhere that the category exist and that your item
>> belong to the category.
>> The get object method seems like a good place to do it. Something like
>> that should do the trick
>>
>>     def get_object(self, queryset=None):
>>         """ Return a 404 code if
>>             * no link with given slug/id
>>             * no category with given slug
>>             * link isn't part of the right category
>>         """"
>>         link = get_object_or_404(Link,
>> pk=self.kwargs['pk'],slug=self.kwargs['slug'])
>>         category = get_object_or_404(Category,
>> slug=self.kwargs['category'])
>>         if not category in link.category_set:
>>             raise Http404
>>         return link
>>
>> 2016-08-21 11:01 GMT+02:00 'voger' via Django users
>> <[email protected]>:
>> > I barely understand django my self but I hope I am pointing you in the
>> > right
>> > direction.
>> >
>> > 1. I don't understand what the (?:/(?P<slug>[\w\d-]+))?/ part in your
>> > pattern is supposed to do. My regexp-fu is weak so I can't comment on
>> > that
>> >
>> > 2. Your url will match and pass to the view whatever url you give it. So
>> > even if you give a pk and slug that don't exist in database, still the
>> > view
>> > will get called as long as the regexp matches.
>> >
>> > 3. In your view you check the db against the pk and slug and if not
>> > found
>> > __the view__ rises 404.
>> >
>> > 4. You don't check if category exist in database and you don't rise 404
>> > if
>> > not. In this case it is assumed it is always right.
>> >
>> >
>> > On 20/08/2016 06:10 μμ, Yunus wrote:
>> >>
>> >> Hello,
>> >>
>> >> I want to two slugs in one URL pattern. These slugs from different
>> >> models. I have a model Link with a many to one relationship with a
>> >> model
>> >> category.
>> >>
>> >> Actually these two slugs is working. But one of the slugs is accepting
>> >> whatever I write in the category_slug section of the url.
>> >>
>> >> Let's say:I
>> >> write 127.0.0.1:8000/there_is_no_name_like_that_in_the_database/pk/slug
>> >> this.
>> >> I am going to this page but there is no category with this name. So,
>> >> basically is accepting whatever I write.
>> >> *#links/views.py*
>> >> |
>> >> classLinkDetailView(FormMixin,DetailView):
>> >>     model =Link
>> >>     context_object_name ='link'
>> >>     form_class =CommentForm
>> >>     success_url =reverse_lazy('home')
>> >>
>> >>
>> >>     defget_object(self,queryset=None):
>> >>
>> >>
>> >>
>> >> returnget_object_or_404(Link,pk=self.kwargs['pk'],slug=self.kwargs['slug'])
>> >>
>> >>
>> >>     ...
>> >> |
>> >>
>> >> *#links/urls.py*
>> >> |
>> >> urlpatterns =[
>> >>
>> >>
>> >>     ...
>> >>
>> >>     url(
>> >>
>> >> regex=r'^k/(?P<category>[\w-]+)/(?P<pk>\d+)(?:/(?P<slug>[\w\d-]+))?/$',
>> >>         view=views.LinkDetailView.as_view(),
>> >>         name='link_detail'
>> >>     ),
>> >>
>> >>
>> >>     ...
>> >> ]
>> >> |
>> >>
>> >> --
>> >> 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]
>> >> <mailto:[email protected]>.
>> >> To post to this group, send email to [email protected]
>> >> <mailto:[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/0b8482ee-5e64-4ee8-a664-d1d938767bd6%40googlegroups.com
>> >>
>> >>
>> >> <https://groups.google.com/d/msgid/django-users/0b8482ee-5e64-4ee8-a664-d1d938767bd6%40googlegroups.com?utm_medium=email&utm_source=footer>.
>> >> For more options, visit https://groups.google.com/d/optout.
>> >
>> >
>> > --
>> > 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/c9904d3f-f162-cfa5-c9e7-b53330225742%40yahoo.gr.
>> >
>> > For more options, visit https://groups.google.com/d/optout.
>>
>>
>>
>> --
>>
>> Cordialement, Coues Ludovic
>> +336 148 743 42
>
> --
> 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/0e73ba29-be8d-46a2-aaa2-04cac94bfffa%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



-- 

Cordialement, Coues Ludovic
+336 148 743 42

-- 
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/CAEuG%2BTao7XuJJbkFzP_mKu9kcDXNyPs%2BS2jSVzi2M%2BGiFAFrPw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to