Skip to content

Commit

Permalink
Created a post model with its fields, migrated it, and registered it …
Browse files Browse the repository at this point in the history
…in the admin interface with customization.
  • Loading branch information
yahyaaly151989 committed Aug 20, 2023
1 parent d38ead5 commit 6c4e429
Show file tree
Hide file tree
Showing 16 changed files with 80 additions and 2 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
12 changes: 11 additions & 1 deletion Module01/mysite/blog/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
from django.contrib import admin
from .models import Post

# Register your models here.

@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
list_display = ['title', 'slug', 'author', 'publish', 'status']
list_filter = ['status', 'created', 'publish', 'author']
search_fields = ['title', 'body']
prepopulated_fields = {'slug': ('title',)}
raw_id_fields = ['author']
date_hierarchy = 'publish'
ordering = ['status', 'publish']
36 changes: 36 additions & 0 deletions Module01/mysite/blog/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Generated by Django 4.2.4 on 2023-08-20 07:26

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


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=250)),
('slug', models.SlugField(max_length=250)),
('body', models.TextField()),
('publish', models.DateTimeField(default=django.utils.timezone.now)),
('created', models.DateTimeField(auto_now_add=True)),
('updated', models.DateTimeField(auto_now=True)),
('status', models.CharField(choices=[('DF', 'Draft'), ('PB', 'Published')], default='DF', max_length=2)),
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='blog_posts', to=settings.AUTH_USER_MODEL)),
],
options={
'ordering': ['-publish'],
'indexes': [models.Index(fields=['-publish'], name='blog_post_publish_bb7600_idx')],
},
),
]
Binary file not shown.
Binary file not shown.
26 changes: 25 additions & 1 deletion Module01/mysite/blog/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User

# Create your models here.
class Post(models.Model):

class Status(models.TextChoices):
DRAFT = 'DF', 'Draft'
PUBLISHED = 'PB', 'Published'

title = models.CharField(max_length=250)
slug = models.SlugField(max_length=250)
author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='blog_posts')
body = models.TextField()
publish = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=2, choices=Status.choices, default=Status.DRAFT)

class Meta:
ordering = ['-publish']
indexes = [
models.Index(fields=['-publish']),
]

def __str__(self):
return self.title
8 changes: 8 additions & 0 deletions Module01/mysite/blog/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.urls import path
from . import views

app_name = 'blog'

urlpatterns = [

]
Binary file added Module01/mysite/db.sqlite3
Binary file not shown.
Binary file modified Module01/mysite/mysite/__pycache__/settings.cpython-311.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 6c4e429

Please sign in to comment.