Skip to content

Commit

Permalink
Create a blog post model
Browse files Browse the repository at this point in the history
Ref #16
  • Loading branch information
Martolivna committed Sep 8, 2023
1 parent 825bb1a commit 4987822
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
29 changes: 29 additions & 0 deletions blog/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 3.2.18 on 2023-08-22 09:14

import django.db.models.deletion
import django.utils.timezone
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name='Post',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=200)),
('text', models.TextField()),
('created_date', models.DateTimeField(default=django.utils.timezone.now)),
('published_date', models.DateTimeField(blank=True, null=True)),
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]
Empty file added blog/migrations/__init__.py
Empty file.
25 changes: 25 additions & 0 deletions blog/models.py
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
"""Models for the blog app."""

from django.conf import settings
from django.db import models
from django.utils import timezone


class Post(models.Model):
"""A class to represent a post."""

author = models.ForeignKey(
settings.AUTH_USER_MODEL, on_delete=models.CASCADE,
)
title = models.CharField(max_length=200)
text = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
published_date = models.DateTimeField(blank=True, null=True)

def publish(self):
"""Save the post entry."""
self.published_date = timezone.now
self.save()

def __str__(self):
"""Return the post title."""
return str(self.title)

0 comments on commit 4987822

Please sign in to comment.