简体   繁体   English

django模型数据库架构

[英]django models database schema

In Django, how would a database schema look like for a social media site like Instagram? 在Django中,像Instagram这样的社交媒体网站的数据库架构看起来如何? For example, how would you make it so that one user can post multiple posts, and they can only have 1 profile? 例如,您如何使一个用户可以发布多个帖子,而他们只能拥有1个个人资料?

  • I want to know what different tables I should have 我想知道我应该拥有哪些不同的表
  • I want to know how I would connect the tables 我想知道如何连接桌子
  • I want to know what I should write to link profile to posts(ie Foreign Key) 我想知道我应该写些什么来将个人资料链接到帖子(即外键)

Any help is appreciated. 任何帮助表示赞赏。

for profiles you add the user as a OneToOneField : 对于配置文件,将用户添加为OneToOneField:

class Profile(models.model):
 User=models.OneToOneField ()

 Phone = models.CharField(default='', max_length=20)
 Zip_code=models.IntegerField(default='')
 image = models.ImageField(blank=True,upload_to='users_photos',)

and for post you add the user as a foreign key: 对于发布,您将用户添加为外键:

class Post(models.Model):
  User=models.ForeignKey(
    User,
    on_delete=models.CASCADE,
   )
  ....and some other fields you want to add

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM