H
H
happiva2019-06-21 07:07:34
Django
happiva, 2019-06-21 07:07:34

How to pass an object as a reverse parameter to a function?

Good afternoon everyone,
I'm implementing an article rating system.
I'm trying to call reverse with the argument of the object I'm on and, specifying self.object as an argument, the name of the article arrives, although the primary_key is on the slug field of the article model
A, it is the object itself that is needed to make it then use in generic foreign key.
I don't understand how to handle this exception I'm
attaching probably the most important one

view.py
vote_form_url = reverse(
                    'add_like', args=[str(self.object)]
                )

def add_like(request, obj):
    obj_type = ContentType.objects.get_for_model(obj)
    like, is_created = Like.objects.get_or_create(
        content_type=obj_type, art_ident=obj.slug, author_id=request.user)
    return reverse('art_detail')


models.py
class Like(models.Model):
    author_id = models.ForeignKey(
        MyUser,
        on_delete=models.CASCADE
    )
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    art_ident = models.ForeignKey(
        Art,
        on_delete=models.CASCADE,
    )
    data_vote = models.DateTimeField(
        auto_now=True
    )
    content_object = GenericForeignKey('content_type', 'art_ident')

    objects = VoteManager()

Thanks in advance

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question