“蓝鲸Django: 显示用户的评论与点赞”的版本间的差异
来自CloudWiki
(创建页面,内容为“==实现点赞功能== ===点赞功能后台接口=== moments.models.py: <nowiki> class Reply(models.Model): status = models.ForeignKey(Status,models.CASCADE)…”) |
(→实现点赞功能) |
||
第7行: | 第7行: | ||
status = models.ForeignKey(Status,models.CASCADE) | status = models.ForeignKey(Status,models.CASCADE) | ||
author = models.CharField(max_length=50) | author = models.CharField(max_length=50) | ||
− | type = models.CharField(max_length= | + | type = models.CharField(max_length=20,choices=[("0","like"),("1","comment")]) |
− | text = models.CharField(max_length= | + | at_person = models.CharField(max_length=50,null=True,blank=True) |
+ | text = models.CharField(max_length=280,null=True,blank=True) | ||
def __str__(self): | def __str__(self): | ||
− | return "{} on {}".format(self.author,self.status) | + | return "{} on {}".format(self.author,self.status.text) |
+ | |||
+ | </nowiki> | ||
+ | |||
+ | ===生成表=== | ||
+ | [root@localhost wechat]# python3 manage.py makemigrations | ||
+ | |||
+ | <nowiki>Migrations for 'moments': | ||
+ | moments/migrations/0002_auto_20200614_0926.py | ||
+ | - Create model Reply | ||
+ | - Change Meta options on status | ||
+ | - Alter field motto on wechatuser | ||
+ | - Alter field pic on wechatuser | ||
+ | - Add field status to reply</nowiki> | ||
+ | |||
+ | |||
+ | [root@localhost wechat]# python3 manage.py migrate | ||
+ | |||
+ | <nowiki>Operations to perform: | ||
+ | Apply all migrations: admin, auth, contenttypes, moments, sessions | ||
+ | Running migrations: | ||
+ | Applying moments.0002_auto_20200614_0926... OK</nowiki> | ||
+ | ===在admin中管理Reply表=== | ||
+ | admin.py: | ||
+ | |||
+ | <nowiki> | ||
+ | from django.contrib import admin | ||
+ | from .models import WeChatUser, Status, Reply | ||
+ | # Register your models here. | ||
+ | |||
+ | |||
+ | admin.site.register(WeChatUser) | ||
+ | admin.site.register(Status) | ||
+ | admin.site.register(Reply) | ||
+ | |||
+ | |||
</nowiki> | </nowiki> |
2020年6月14日 (日) 01:31的版本
实现点赞功能
点赞功能后台接口
moments.models.py:
class Reply(models.Model): status = models.ForeignKey(Status,models.CASCADE) author = models.CharField(max_length=50) type = models.CharField(max_length=20,choices=[("0","like"),("1","comment")]) at_person = models.CharField(max_length=50,null=True,blank=True) text = models.CharField(max_length=280,null=True,blank=True) def __str__(self): return "{} on {}".format(self.author,self.status.text)
生成表
[root@localhost wechat]# python3 manage.py makemigrations
Migrations for 'moments': moments/migrations/0002_auto_20200614_0926.py - Create model Reply - Change Meta options on status - Alter field motto on wechatuser - Alter field pic on wechatuser - Add field status to reply
[root@localhost wechat]# python3 manage.py migrate
Operations to perform: Apply all migrations: admin, auth, contenttypes, moments, sessions Running migrations: Applying moments.0002_auto_20200614_0926... OK
在admin中管理Reply表
admin.py:
from django.contrib import admin from .models import WeChatUser, Status, Reply # Register your models here. admin.site.register(WeChatUser) admin.site.register(Status) admin.site.register(Reply)