Westagram

Hyerang Kim·2020년 5월 24일
0

We cloned the instagram's basic backend server part as an individual assignment, but we ended up having a group to create and pass over the API to the frontend to see the web flow for real.

Main functionalities

  1. Sign-in
  2. Sign-up
  3. Comment

Overview

  • Django project initial settings

  • Edit settings.py

  • Create an account app and comment app

  • Modeling in models.py

  • Create an endpoint in urls.py

  • Write funtions in views.py

Essential codes to remember

In comment/models.py, username field is a Foreign Key that refers to the username in Users class in account/models.py.

from django.db import models
from account.models import Users

class Comment(models.Model):
        username = models.ForeignKey(Users, on_delete = models.SET_NULL, null=True)
        comment = models.TextField()
        created_at = models.DateTimeField(auto_now_add = True)
        updated_at = models.DateTimeField(auto_now = True)


        class Meta:
                db_table = 'comment'

ForeignKey.on_delete

It accepts other arguments that define the details of how the relation works. When an object referenced by a ForeignKey is deleted, Django will emulate the behavior of the SQL constraint specified by the on_delete argument.

The possible values for on_delete are found in django.db.models:

  • CASCADE
    Cascade deletes. Django emulates the behavior of the SQL constraint ON DELETE CASCADE and also deletes the object containing the ForeignKey.
    Model.delete() isn’t called on related models, but the pre_delete and post_delete signals are sent for all deleted objects.

  • PROTECT
    Prevent deletion of the referenced object by raising ProtectedError, a subclass of django.db.IntegrityError.

  • SET_NULL
    Set the ForeignKey null; this is only possible if null is True.

  • SET_DEFAULT
    Set the ForeignKey to its default value; a default for the ForeignKey must be set.

  • SET()
    Set the ForeignKey to the value passed to SET(), or if a callable is passed in, the result of calling it. In most cases, passing a callable will be necessary to avoid executing queries at the time your models.py is imported:

Make sure to include Meta class that indicates database table name.

profile
Backend Developer

0개의 댓글