forked from yahyaaly151989/Mastering_Django
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created a post model with its fields, migrated it, and registered it …
…in the admin interface with customization.
- Loading branch information
1 parent
d38ead5
commit 6c4e429
Showing
16 changed files
with
80 additions
and
2 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 added
BIN
+2.2 KB
Module01/mysite/blog/migrations/__pycache__/0001_initial.cpython-311.pyc
Binary file not shown.
Binary file added
BIN
+182 Bytes
Module01/mysite/blog/migrations/__pycache__/__init__.cpython-311.pyc
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Binary file modified
BIN
+22 Bytes
(100%)
Module01/mysite/mysite/__pycache__/settings.cpython-311.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.