강의

멘토링

커뮤니티

Inflearn Community Q&A

hyukster90666's profile image
hyukster90666

asked

Introduction to Web Development for Python Users A to Z Django + Bootstrap

Tag model description, creation & model test code writing

test_tag 질문드립니다.

Written on

·

202

0

    def test_tag(self):

tag_000 = create_tag(name='bad_guy')

tag_001 = create_tag(name='ameraica')

Post_000 = create_post(

'The first post',

'Hello World! we are the world!',

self.author_000,

)

Post_000.tags.add(tag_000)

Post_000.tags.add(tag_001)

Post_000.save()

Post_001 = create_post(

'Stay Fool, Stay Hungry',

'Story about Steve Jobs',

self.author_000,

)

Post_001.tags.add(tag_001)

Post_001.save()

# post는 여러개의 태그를 가질 수 있는지 확인

self.assertEqual(Post_000.tags.count(), 2)

# tag에 여러개의 포스트가 연결되어 있는지 확인

self.assertEqual(tag_001.post_set.count(), 2)

print("태그01에 걸린 포스트의 개수는?")

print(tag_001.post_set.count())

print("태그01에 걸린 첫번째 포스트")

print(tag_001.post_set.first())

print('태그01에 걸린 두 번째 포스트')

print(tag_001.post_set.last())

self.assertEqual(tag_001.post_set.first(), Post_000)

self.assertEqual(tag_001.post_set.last(), Post_001)

이렇게 했는데요!! 이상하게 first에 Post_001이 나오고 last에 Post_000이 나와요.

뭐가 잘못된걸까요?ㅠㅠㅠ

pythonbootstrapdjango

Answer 1

0

SungYong Lee님의 프로필 이미지
SungYong Lee
Instructor

오타가 없다면, 여기에 써주신 내용만 봐서는 문제가 없어보이는데요.

Post에 혹시 아래 내용을 넣어주셨다면 그럴 수도 있습니다. 하지만 여기까지는 아래 내용을 배우지 않았기 때문에 그럴리는 없을 것 같고요...

    class Meta:

ordering = ('-created',)

전체 코드를 봐야 알 수 있을 것 같습니다.

hyukster90666's profile image
hyukster90666

asked

Ask a question