简体   繁体   中英

django models database schema

In Django, how would a database schema look like for a social media site like Instagram? For example, how would you make it so that one user can post multiple posts, and they can only have 1 profile?

  • 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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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