首页 > 代码库 > python16_day20【Django_继续抽屉项目】

python16_day20【Django_继续抽屉项目】

一、djangoAdmin和表结构

  1.项目名称

    python manage startapp web    # 前端页面

    python manage startapp repository   # 只是数据库

    python manage startapp backend      # 后台管理

  2.repository/models.py

 1 class UserInfo(models.Model):
 2     nid = models.AutoField(primary_key=True)
 3     username = models.CharField(max_length=32, unique=True)
 4     password = models.CharField(max_length=32)
 5     email = models.CharField(max_length=32, unique=True)
 6     ctime = models.DateTimeField()
 7 
 8     class Meta:
 9         verbose_name_plural = 用户表‘    # 在django中显示表名称
10 
11     def __str__(self):
12         return self.username        # 显示字段对象名称
13 class NewsType(models.Model):
14     nid = models.AutoField(primary_key=True)
15 
16     caption = models.CharField(max_length=32)
17     class Meta:
18         verbose_name_plural = "新闻类型"
19 
20     def __str__(self):
21         return self.caption
22 
23 class News(models.Model):
24     nid = models.AutoField(primary_key=True)
25     user_info = models.ForeignKey(UserInfo)
26     news_type = models.ForeignKey(NewsType)
27     title = models.CharField(max_length=32, db_index=True)
28     url = models.CharField(max_length=128, null=True,blank=True)
29     content = models.CharField(max_length=50)
30     favor_count = models.IntegerField(default=0)
31     comment_count = models.IntegerField(default=0)
32     ctime = models.DateTimeField()
33     class Meta:
34         verbose_name_plural = 新闻
35 
36     def __str__(self):
37         return self.title
38 class Favor(models.Model):
39     nid = models.AutoField(primary_key=True)
40 
41     user_info = models.ForeignKey(UserInfo)
42     news = models.ForeignKey(News)
43 
44     ctime = models.DateTimeField()
45 
46     class Meta:
47         verbose_name_plural = 点赞记录
48         unique_together = (
49             ("user_info", "news"),
50         )
51 
52 class Comment(models.Model):
53     nid = models.AutoField(primary_key=True)
54 
55     user_info = models.ForeignKey(UserInfo)
56     news = models.ForeignKey(News)
57 
58     ctime = models.DateTimeField()
59     device = models.CharField(max_length=16,null=True,blank=True)
60     content = models.CharField(max_length=150)
61 
62     reply_id = models.ForeignKey(Comment, related_name=b, null=True, blank=True)
63 
64     class Meta:
65         verbose_name_plural = 评论表

  3.repository/admin.py

1 from django.contrib import admin
2 from repository.models import *
3 
4 # Register your models here.
5 admin.site.register(UserInfo)
6 admin.site.register(Comment)
7 admin.site.register(Favor)
8 admin.site.register(News)
9 admin.site.register(NewsType)

  4. python manage.py createsuperuser

  5. settings.py 注册项目  

1 INSTALLED_APPS = [
2     django.contrib.admin,
3     django.contrib.auth,
4     django.contrib.contenttypes,
5     django.contrib.sessions,
6     django.contrib.messages,
7     django.contrib.staticfiles,
8     ‘repository‘,
9 ]

  6.python manage.py makemigrations

   python manage.py migrate

  7.http://127.0.0.1:8000/admin

 

python16_day20【Django_继续抽屉项目】