我已经通过使用Eric Matthes的* Python Crash Course *学习了一些Django,偶然发现了以下问题。即,我有以下代码:
class Topic(models.Model):
text = models.CharField(max_length=200)
public = models.BooleanField(default=False)
public_2 = False
def __str__(self):
return self.text
然后,在视图函数中,如果我设置了public = True
和public_2 = True
,并且在外壳中进行了验证,那么我将拥有topic.public = True
,但是拥有public_2 = False
。为什么会这样,有什么方法可以更改public_2
属性?这是查看功能:
def new_entry(request, topic_id):
topic = get_object_or_404(Topic, id=topic_id)
if topic.owner != request.user:
raise Http404
if request.method != 'POST':
# No data submitted; create a blank form.
form = EntryForm()
else:
# POST data submitted; process data
form = EntryForm(data=request.POST)
if form.is_valid():
new_entry = form.save(commit=False)
new_entry.topic = topic
new_entry.public = True
new_entry.public_2 = True
new_entry.save()
return HttpResponseRedirect(reverse('learning_logs:topic',
args=[topic_id]))
context = {'topic': topic, 'form': form}
return render(request, 'learning_logs/new_entry.html', context)
如果没有办法更改,是否可以更改Model类的哪些类型的其他限制?