# models.py ,
# 게시글의 url을 받아내는 get_absolute_url 함수를 만들어준다.
# 강의에서는 pk 값을 썼지만, 나는 article_id를 사용했다.
class Article(models.Model):
class Meta:
db_table = "Article"
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="user")
title = models.CharField(max_length=50, verbose_name="제목")
content = models.TextField(verbose_name="내용")
hearts = models.ManyToManyField(User, blank=True, related_name="hearts")
created_at = models.DateTimeField(auto_now_add=True, verbose_name="생성시간")
updated_at = models.DateTimeField(auto_now=True, verbose_name="수정시간")
# 이미지
uploaded_image = models.ImageField(
upload_to=rename_imagefile_to_uuid, verbose_name="업로드이미지", blank=True, null=True
)
def __str__(self):
return str(self.title)
def get_absolute_url(self):
return reverse('article_detail_view', kwargs={"article_id":self.article_id})
# tests.py
class ArticleReadTest(APITestCase):
@classmethod
def setUpTestData(cls):
cls.faker = Faker()
cls.articles = []
cls.rnd_email = f"{last_name}@{faker.domain_name()}"
for i in range(10):
cls.user = User.objects.create_user(cls.rnd_email, cls.faker.first_name(), cls.faker.last_name(), cls.faker.word())
cls.articles.append(Article.objects.create(title=cls.faker.sentence(), content=cls.faker.text(), user=cls.user))
def test_get_article(self):
for article in self.articles:
url = article.get_absolute_url()
response = self.client.get(url)
serializer = ArticleCreateSerializer(article).data
for key, value in serializer.items():
여기서 items() 함수를 설명하기 위해서 다음강(20강)으로 넘어갔다.