Skip to content

Commit b005175

Browse files
committed
优化表结构和post视图代码
1 parent 037ac57 commit b005175

File tree

8 files changed

+23
-38
lines changed

8 files changed

+23
-38
lines changed

.idea/dataSources.ids

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/dataSources.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

db.sqlite3

0 Bytes
Binary file not shown.

main/models.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,16 @@ def is_following(self, user):
2828
return bool(self.followers.filter(followed_id=user.id))
2929

3030
def followed_posts(self):
31-
l1 = []
32-
l2 = []
33-
f = self.followers.all()
34-
for i in f:
35-
l1.append(i.followed.id)
36-
posts = Post.objects.filter(author__in=l1).order_by('-timestamp')
37-
for i in posts:
38-
l2.append(i)
39-
return l2
31+
return Post.objects.select_related().filter(author=self.followers.all()).order_by('-timestamp')
4032

4133

4234
class Post(models.Model):
4335
body = models.TextField()
4436
timestamp = models.DateTimeField(db_index=True, auto_now_add=True)
45-
author = models.ForeignKey(User, related_name='posts')
37+
author = models.ForeignKey(UserProfile, related_name='posts')
4638

4739
def __unicode__(self):
48-
return self.author.username
40+
return self.author.user.username
4941

5042

5143
class Follows(models.Model):
@@ -54,7 +46,7 @@ class Follows(models.Model):
5446
timestamp = models.DateTimeField(auto_now=True)
5547

5648
def __unicode__(self):
57-
return self.follower.user.username
49+
return self.followed.user.username
5850

5951
class Meta:
6052
unique_together = (("follower", "followed"),)
@@ -63,7 +55,7 @@ class Meta:
6355
class Comment(models.Model):
6456
body = models.TextField()
6557
timestamp = models.DateTimeField(db_index=True, auto_now_add=True)
66-
author = models.ForeignKey(User)
58+
author = models.ForeignKey(UserProfile, related_name='comments')
6759
post = models.ForeignKey(Post, related_name='comments')
6860

6961
def __unicode__(self):

main/views.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,17 @@ def index(request):
1717
p.body = body
1818
p.save()
1919
return HttpResponseRedirect('/')
20-
postsObj = []
21-
posts = Post.objects.order_by('-timestamp')
22-
for i in posts:
23-
postsObj.append(i)
24-
show_cookie = False
20+
show_followed = False
2521
if str(request.user) != 'AnonymousUser':
2622
u = UserProfile.objects.get(user__username=request.user)
2723
show_cookie = bool(request.COOKIES.get('show_followed', ''))
2824
if show_cookie:
2925
posts = u.followed_posts()
30-
if not show_cookie:
31-
posts = []
32-
postsObj = Post.objects.order_by('-timestamp')
33-
for i in postsObj:
34-
posts.append(i)
26+
else:
27+
posts = Post.objects.order_by('-timestamp')
3528
return render_to_response(
3629
'index.html',
37-
{'posts': posts, 'show_followed': show_cookie},
30+
locals(),
3831
context_instance=RequestContext(request))
3932

4033

templates/_comments.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<li class="comment">
77
<div class="comment-content">
88
<div class="comment-date">{{ comment.timestamp|date:"Y-m-d H:i:s" }}</div>
9-
<div class="comment-author"><a href="/user/{{ comment.author.username }}">{{ comment.author.username }}</a></div>
9+
<div class="comment-author"><a href="/user/{{ comment.author.user.username }}">{{ comment.author.user.username }}</a></div>
1010
<div class="comment-body">{{ comment.body }}</div>
1111
</div>
1212
</li>

templates/_posts.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<li class="post">
66
<div class="post-content">
77
<div class="post-date">{{ post.timestamp|date:"Y-m-d H:i:s" }}</div>
8-
<div class="post-author"><a href="/user/{{ post.author.username }}">{{ post.author.username }}</a></div>
8+
<div class="post-author"><a href="/user/{{ post.author.user.username }}">{{ post.author.user.username }}</a></div>
99
<div class="post-body">{{ post.body }}</div>
1010
</div>
1111
<div class="post-footer">

templates/post.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<li class="post">
88
<div class="post-content">
99
<div class="post-date">{{ post.timestamp|date:"Y-m-d H:i:s" }}</div>
10-
<div class="post-author"><a href="/user/{{ post.author_id.username }}">{{ post.author_id.username }}</a></div>
10+
<div class="post-author"><a href="/user/{{ post.author.user.username }}">{{ post.author.user.username }}</a></div>
1111
<div class="post-body">{{ post.body }}</div>
1212
</div>
1313
</li>

0 commit comments

Comments
 (0)