Python 如果没有,则自动使用值填充django模型字段
我试图自动填充我的注释模型的注释器字段,但没有填充由“Anon”和注释主键组成的字符串值(我认为应该在django中自动生成)。我试图通过重写save()函数来实现这一点。这是我的模型代码Python 如果没有,则自动使用值填充django模型字段,python,django,django-models,django-views,Python,Django,Django Models,Django Views,我试图自动填充我的注释模型的注释器字段,但没有填充由“Anon”和注释主键组成的字符串值(我认为应该在django中自动生成)。我试图通过重写save()函数来实现这一点。这是我的模型代码 from django.db import models from django.contrib.auth.models import User from ckeditor.fields import RichTextField from imagekit.models import ProcessedIma
from django.db import models
from django.contrib.auth.models import User
from ckeditor.fields import RichTextField
from imagekit.models import ProcessedImageField, ImageSpecField
from imagekit.processors import ResizeToFill
# Create your models here.
STATUS = (
(0, "Draft"),
(1, "Publish")
)
class Post(models.Model):
title = models.CharField(max_length=200, unique=True)
slug = models.SlugField(max_length=200, unique=True)
cover_image = ProcessedImageField(upload_to='images/%Y/%m/%d/',
blank=True,
null=True,
processors=[ResizeToFill(250, 250)],
format='JPEG',
options={'quality': 90})
author = models.ForeignKey(User, on_delete= models.CASCADE, related_name='blog_posts')
updated_on = models.DateTimeField(auto_now=True)
content = RichTextField()
created_on = models.DateTimeField(auto_now_add=True)
status = models.IntegerField(choices=STATUS, default=0)
class Meta:
ordering = ['-created_on']
def __str__(self):
return self.title
class Comment(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name="comments")
created_on = models.DateTimeField(auto_now_add=True)
commenter = models.CharField(max_length=20, blank=True, help_text="Fill this out, or don't")
comment_content = models.TextField(max_length=300)
active = models.BooleanField(default=False)
class Meta:
ordering = ['-created_on']
def __str__(self):
return f"Comment by {self.commenter} on {self.post}"
def save(self, *args, **kwargs):
if self.commenter == None:
self.commenter = "Anon" + str(self.pk)
return self.commenter
else:
super().save(*args, **kwargs)
如代码所示,如果未输入注释器字段,我将尝试使用字符串“Anon”+主键填充注释器字段。我正在基于功能的post_详细视图中保存注释。这是代码
from django.shortcuts import render, get_object_or_404
from django.views import generic
from .models import Post, Comment
from .forms import CommentForm
# Create your views here.
class HomePageView(generic.base.TemplateView):
template_name = "main.html"
class PostList(generic.ListView):
queryset = Post.objects.filter(status=1).order_by('-created_on')
template_name = 'blog.html'
def post_detail(request, slug):
template_name = 'post_detail.html'
post = get_object_or_404(Post, slug=slug)
comments = post.comments.filter(active=True)
new_comment = None
if request.method == 'POST':
comment_form = CommentForm(data=request.POST)
if comment_form.is_valid():
new_comment = comment_form.save(commit=False)
new_comment.post = post
new_comment.save()
else:
comment_form = CommentForm()
return render(request, template_name, {'post':post,
'comments': comments,
'new_comment': new_comment,
'comment_form': comment_form})
感谢您的帮助。现在我的两个想法是,a。修改我的post_详细视图以合并逻辑,或b。在模型上设置默认值。我不确定正确的方法,因此非常感谢django向导的任何输入。谢谢所有能帮忙的人