Django 为用户轮询应用程序管理员,就像表单一样



我遵循了 django 文档入门应用程序,并使用 django.auth 添加了登录和注册系统。我希望这样做,以便已登录的用户可以创建新的民意调查并将选择自动链接到问题。就像在 django 管理面板中完成的那样(见图(。

在 admin.py 中,您使用管理员。表格内联和字段集,但我不确定如何在 forms.py 或 views.py 中做到这一点,我似乎在文档或其他任何地方都找不到太多东西,所以如果有人可以得到,那就太好了..

admin.py

from django.contrib import admin
from .models import Question, Choice

class ChoiceInLine(admin.TabularInline):
model = Choice
extra = 3

class QuestionAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['question_text']}),
('Date information', {'fields': ['date_posted']})
]
inlines = [ChoiceInLine]
list_display = ('question_text', 'date_posted',  'was_published_recently')
list_filer = 'date_posted'

admin.site.register(Question, QuestionAdmin)

models.py

from django.db import models
from django.utils import timezone
import datetime

class Question(models.Model):
question_text = models.CharField(max_length=200)
date_posted = models.DateTimeField('Date published')
def was_published_recently(self):
now = timezone.now()
return now - datetime.timedelta(days=1) <= self.date_posted <= now
was_published_recently.admin_order_field = 'date_posted'
was_published_recently.boolean = True
was_published_recently.short_description = 'Posted recently?'
def __str__(self):
return self.question_text

class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=100)
votes = models.IntegerField(default=0)
def __str__(self):
return self.choice_text

我想复制的管理表格的照片

我想这就是你要的。如果你想自己做这件事,我建议你查看 django 管理表单的源代码(答案中的选项 2(。您可以从这里开始研究。

相关内容

最新更新