Added forum model
This commit is contained in:
parent
04f66506ec
commit
184b7e2e0a
@ -39,6 +39,7 @@ INSTALLED_APPS = [
|
|||||||
'django.contrib.sessions',
|
'django.contrib.sessions',
|
||||||
'django.contrib.messages',
|
'django.contrib.messages',
|
||||||
'django.contrib.staticfiles',
|
'django.contrib.staticfiles',
|
||||||
|
'forum.apps.ForumConfig',
|
||||||
'main.apps.MainConfig',
|
'main.apps.MainConfig',
|
||||||
'newsletters.apps.NewslettersConfig'
|
'newsletters.apps.NewslettersConfig'
|
||||||
]
|
]
|
||||||
@ -112,7 +113,7 @@ AUTH_PASSWORD_VALIDATORS = [
|
|||||||
|
|
||||||
LANGUAGE_CODE = 'en-us'
|
LANGUAGE_CODE = 'en-us'
|
||||||
|
|
||||||
TIME_ZONE = 'UTC'
|
TIME_ZONE = 'Canada/Eastern'
|
||||||
|
|
||||||
USE_I18N = True
|
USE_I18N = True
|
||||||
|
|
||||||
|
BIN
db.sqlite3
BIN
db.sqlite3
Binary file not shown.
0
forum/__init__.py
Normal file
0
forum/__init__.py
Normal file
50
forum/admin.py
Normal file
50
forum/admin.py
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
from django.contrib import admin
|
||||||
|
|
||||||
|
from .models import Thread, Post, Comment
|
||||||
|
|
||||||
|
|
||||||
|
admin.site.register(Thread)
|
||||||
|
|
||||||
|
|
||||||
|
@admin.register(Post)
|
||||||
|
class PostAdmin(admin.ModelAdmin):
|
||||||
|
list_display = ['title', 'topic', 'creator', 'created']
|
||||||
|
search_fields = ['title', 'topic', 'creator', 'created']
|
||||||
|
|
||||||
|
def get_form(self, request, obj=None, **kwargs):
|
||||||
|
if obj:
|
||||||
|
self.fields = ['creator', 'created', 'topic', 'title', 'question']
|
||||||
|
self.readonly_fields = ['creator', 'created']
|
||||||
|
else:
|
||||||
|
self.fields = ['topic', 'title', 'question']
|
||||||
|
self.readonly_fields = []
|
||||||
|
return super(PostAdmin, self).get_form(request, obj, **kwargs)
|
||||||
|
|
||||||
|
def save_model(self, request, instance, form, change):
|
||||||
|
user = request.user
|
||||||
|
instance = form.save(commit=False)
|
||||||
|
instance.creator = user
|
||||||
|
instance.save()
|
||||||
|
return instance
|
||||||
|
|
||||||
|
|
||||||
|
@admin.register(Comment)
|
||||||
|
class CommentAdmin(admin.ModelAdmin):
|
||||||
|
list_display = ['post', 'creator', 'created']
|
||||||
|
search_fields = ['post', 'creator', 'created']
|
||||||
|
|
||||||
|
def get_form(self, request, obj=None, **kwargs):
|
||||||
|
if obj:
|
||||||
|
self.fields = ['creator', 'created', 'post', 'reply']
|
||||||
|
self.readonly_fields = ['post', 'creator', 'created']
|
||||||
|
else:
|
||||||
|
self.fields = ['post', 'reply']
|
||||||
|
self.readonly_fields = []
|
||||||
|
return super(CommentAdmin, self).get_form(request, obj, **kwargs)
|
||||||
|
|
||||||
|
def save_model(self, request, instance, form, change):
|
||||||
|
user = request.user
|
||||||
|
instance = form.save(commit=False)
|
||||||
|
instance.creator = user
|
||||||
|
instance.save()
|
||||||
|
return instance
|
5
forum/apps.py
Normal file
5
forum/apps.py
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
from django.apps import AppConfig
|
||||||
|
|
||||||
|
|
||||||
|
class ForumConfig(AppConfig):
|
||||||
|
name = 'forum'
|
30
forum/models.py
Normal file
30
forum/models.py
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
from django.db import models
|
||||||
|
from django.contrib.auth.models import User
|
||||||
|
|
||||||
|
|
||||||
|
class Thread(models.Model):
|
||||||
|
topic = models.CharField(max_length=255)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.topic
|
||||||
|
|
||||||
|
|
||||||
|
class Post(models.Model):
|
||||||
|
topic = models.ForeignKey(Thread)
|
||||||
|
title = models.CharField(max_length=255)
|
||||||
|
question = models.TextField()
|
||||||
|
creator = models.ForeignKey(User)
|
||||||
|
created = models.DateTimeField(auto_now_add=True)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.title
|
||||||
|
|
||||||
|
|
||||||
|
class Comment(models.Model):
|
||||||
|
post = models.ForeignKey(Post)
|
||||||
|
reply = models.TextField()
|
||||||
|
creator = models.ForeignKey(User)
|
||||||
|
created = models.DateTimeField(auto_now_add=True)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.reply
|
3
forum/tests.py
Normal file
3
forum/tests.py
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
from django.test import TestCase
|
||||||
|
|
||||||
|
# Create your tests here.
|
3
forum/views.py
Normal file
3
forum/views.py
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
from django.shortcuts import render
|
||||||
|
|
||||||
|
# Create your views here.
|
Loading…
Reference in New Issue
Block a user