Z
Z
zxcv0in2021-05-04 16:30:08
Django
zxcv0in, 2021-05-04 16:30:08

Is self.id created for every model object by default? How does args work in reverse()?

Maybe the question will seem stupid, but I'm stupid. I scoured the documentation and, do not believe it, googled, but did not find a clear answer to these two questions. And now to the specifics:
def get_absolute_url(self):
return reverse("book-detail", args = [str(self.id)])
Firstly, the id variable is not defined in the class, which means it was either defined by default, what, in fact, the question is, or is determined later in the tutorial (I'm still learning, so maybe there will be an answer to this question later). Is self.id created for each instance of the model by default?
Secondly, what does args in reverse() do?
And yet, if it's not difficult, you can explain this construction:
', '.join([ genre.name for genre in self.genre.all()[:3] ])
I don’t specifically understand why genre.name is here and what is being referred to here if the class is only “Genre” and not “genre” (yes, the “Book” model has a “genre” parameter that already refers to a specific class " Genre", which has the "name" parameter defined, but is it possible to handle it this way and will there be a difference if we just put "Genre.name" instead of "genre.name" in this case?
Code:
class Genre(models.Model):
name = models.CharField(max_length = 200, help_text = "chose a genre of the book")

class Meta:
ordering = ["name"]
def __str__(self):
return self.name

from django.urls import reverse

class Book(models. Model):
title = models.CharField(max_length = 1000)
genre = models.ManyToManyField(Genre, help_text = "choose a genre for the book")
author = models.ForeignKey("Author", on_delete = models.SET_NULL, null = True)
summary = models.TextField(max_length=1000, help_text ="Enter a brief description of the book")

def __str__(self):
return self.title

def display_genre(self):
return ', '.join([ genre.name for genre in self.genre.all()[: 3] ])
display_genre.short_description = 'Genre'

def get_absolute_url(self):
return reverse("book-detail", args = [str(self.id)])

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
maksam07, 2021-05-04
@zxcv0in

You have too many questions in one QUESTION
1. id is the default unless another primary key is explicitly set. pk and id in a model where the primary key is not explicitly set will refer to the same field, but if you suddenly make id_customname the primary key, then pk will refer to it. That is, pk is a generic property that looks up the primary key from the model, but works a little slower than if you set id when accessing the row.
2. args substitutes, in fact, the arguments in the book-detail url, but personally I prefer another option:

return reverse('book-detail', kwargs={'id': self.id})

That is, you have (as I understand the tutorial from Mozilla):
url(r'^book/(?P<pk>\d+)$', views.BookDetailView.as_view(), name='book-detail'),

And the link will become: book/1
3.
', '.join([ genre.name for genre in self.genre.all()[:3] ])
Lists the first 3 names in the model separated by commas, as far as I remember the
syntax

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question