我正在建立一个邮政管理系统。因此,垃圾邮件发送者将无法在我的页面上发布垃圾邮件。我创建了一个PostAdmin类,但是它不起作用。
这是我的admin.py
from django.contrib import admin
from .models import Post
class PostAdmin(admin.ModelAdmin):
...
def make_published(self, request, queryset):
rows_updated = queryset.update(status='p')
if rows_updated == 1:
message_bit = "1 story was"
else:
message_bit = "%s stories were" % rows_updated
self.message_user(request, "%s successfully marked as published." % message_bit)
admin.site.register(Post, PostAdmin)
这是我的模特。py
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.urls import reverse
STATUS_CHOICES = (
('d', 'Draft'),
('p', 'Published'),
('w', 'Withdrawn'),
)
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
status = models.CharField(max_length=10, choices= STATUS_CHOICES, default='draft')
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('post-detail', kwargs={'pk': self.pk})```