【发布时间】:2017-05-20 18:09:09
【问题描述】:
为用户提供公开主题的选项。这个会 需要一个名为 public 的属性作为主题模型的一部分(默认情况下应设置为 False)和 new_topic 页面上的表单元素,允许用户将主题从私有更改为公共[在此处输入链接描述][1]
from django.db import models
from django.contrib.auth.models import User
class Topic(models.Model):
"""A topic the user is learning about."""
text = models.CharField(max_length=200)
date_added = models.DateTimeField(auto_now_add=True)
owner = models.ForeignKey(User)
def __str__(self):
"""Return a string representation of the model."""
return self.text
class Entry(models.Model):
"""Something specific learned about a topic."""
topic = models.ForeignKey(Topic)
text = models.TextField()
date_added = models.DateTimeField(auto_now_add=True)
class Meta:
verbose_name_plural = 'entries'
def __str__(self):
"""Return a string representation of the model."""
return self.text[:50] + "..."enter code here
【问题讨论】:
-
有什么问题?
-
在Topic模型中添加一个名为public的属性,需要默认设置为False!
-
下面是E书第382页练习20-5github.com/ehmatthes/pcc/blob/master/chapter_20/learning_logs/…
-
public = models.BooleanField(default=False)
-
您好 Doner,我认为您的方法在 public 属性更改中是正确的。谢谢,但这个问题还没有完全解决。这个问题的第二部分是 new_topic 页面上的表单元素,它允许用户将主题从私有更改为公共,我不确定如何在 new_topic html 文件中进行更改。