官术网_书友最值得收藏!

The model hunt

Here is a first cut at identifying the models in SuperBook. As typical for an early attempt, we have represented only the essential models and their relationships in the form of a simplistic class diagram:

An early attempt at the SuperBook class diagram

Let's forget models for a moment and talk in terms of the objects we are modeling. Each user has a profile. A user can make several comments or several posts. A Like can be related to a single user/post combination.

Drawing a class diagram of your models like this is recommended. Class attributes might be missing at this stage, but you can detail them later. Once the entire project is represented in the diagram, it makes separating the apps easier.

Here are some tips to create this representation:

  • Nouns in your write-up typically end up as entities.
  • Boxes represent entities, which become models.
  • Connector lines are bi-directional and represent one of the three types of relationships in Django: one-to-one, one-to-many (implemented with Foreign Keys), and many-to-many.
  • The field denoting the one-to-many relationship is defined in the model on the Entity-relationship model (ER-model). In other words, the n side is where the Foreign Key gets declared.

The class diagram can be mapped into the following Django code (which will be spread across several apps):

class Profile(models.Model): 
    user = models.OneToOneField(User) 
 
class Post(models.Model): 
    posted_by = models.ForeignKey(User) 
 
class Comment(models.Model): 
    commented_by = models.ForeignKey(User) 
    for_post = models.ForeignKey(Post) 
 
class Like(models.Model): 
    liked_by = models.ForeignKey(User) 
    post = models.ForeignKey(Post) 

Later, we will not reference the User directly, but use the more general settings.AUTH_USER_MODEL instead. We are also not concerned about field attributes such as on_delete or primary_key at this stage. We will get into those details soon.

主站蜘蛛池模板: 乐都县| 顺义区| 呼和浩特市| 舒城县| 巩留县| 太原市| 新巴尔虎左旗| 永吉县| 万载县| 随州市| 汉川市| 龙南县| 正镶白旗| 鄂托克旗| 克拉玛依市| 定边县| 金昌市| 上杭县| 凤凰县| 防城港市| 宜黄县| 峨山| 丹东市| 华阴市| 乌拉特后旗| 贞丰县| 涟源市| 科尔| 昌都县| 贵德县| 平定县| 荔波县| 南丰县| 嘉黎县| 珠海市| 黄陵县| 峨山| 万年县| 彝良县| 辽阳市| 双辽市|