Xxxxxxxxxxs
Xxxxxxxxxxs
采纳率0%
2020-01-31 10:33 阅读 709

<python从入门到实践>19章书中只给topic设定了owner,但是entry没有出现了报错django.db.utils.OperationalError: no such column: learning_logs_entry.owner_id

我用改topic的方法改了new_entry,也没有行通。
views.py

from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.urls import reverse
from .models import Topic,Entry
from .forms import TopicForm,EntryForm
from django.contrib.auth.decorators import login_required

def index(request):
    #学习笔记的主页
    return render(request,'learning_logs/index.html')

@login_required
def topics(request):
    #显示所有的主题
    topics = Topic.objects.filter(owner=request.user).order_by('date_added')
    context = {'topics':topics}
    return render(request,'learning_logs/topics.html',context)

@login_required
def topic(request,topic_id):
    #显示单个主题及其所有的项目
    topic = Topic.objects.filter(owner=request.user).get(id = topic_id)
    #确认请求的主题属于当前用户
    if topic.owner != request.user:
        raise Http404
    entries = topic.entry_set.order_by('-date_added')
    context = {'topic':topic,'entries':entries}
    return render(request,'learning_logs/topic.html',context)

@login_required
def new_topic(request):
    '''添加新主题'''
    if request.method != 'POST':
        #未提交数据 创建一个新表单
        form = TopicForm()
    else:
        #POST提交的数据 对数据进行处理
        form = TopicForm(request.POST)
        if form.is_valid():
            new_topic = form.save(commit=False)
            new_topic.owner = request.user
            new_topic.save()
            return HttpResponseRedirect(reverse('learning_logs:topics'))

    context = {'form':form}
    return render(request, 'learning_logs/new_topic.html',context)

@login_required
def new_entry(request,topic_id):
    topic = Topic.objects.get(id=topic_id)

    if request.method != 'POST':
        form = EntryForm()
    else:
        form = EntryForm(data=request.POST)
        if form.is_valid():
            return HttpResponseRedirect(reverse('learning_logs:topic',args=[topic_id]))
    context = {'topic':topic,'form':form}
    return render(request, 'learning_logs/new_entry.html',context)

@login_required
def edit_entry(request,entry_id):
    '''编辑现有的条目'''
    entry = Entry.objects.get(id=entry_id)
    topic = entry.topic
    if topic.owner != request.user:
        raise Http404

    if request.method != 'POST':
        form = EntryForm(instance=entry)
    else:
        #POST提交的数据,对数据进行处理
        form = EntryForm(instance=entry,data=request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse('learning_logs:topic',args=[topic.id]))

    context = {'entry':entry,
               'topic':topic,
               'form':form
               }
    return render(requ

models.py

from django.db import models
from django.contrib.auth.models import User

class Topic(models.Model):
    #用户学习主题
    text = models.CharField(max_length= 200)
    date_added = models.DateTimeField(auto_now_add=True)
    owner = models.ForeignKey(User,on_delete=models.CASCADE)

    def __str__(self):
        #返回模型的字符串表示
        return self.text

class Entry(models.Model):
    #学到的有关某个主题的具体知识
    topic = models.ForeignKey(Topic,on_delete=models.CASCADE)
    text = models.TextField()
    date_added = models.DateTimeField(auto_now_add= True)
    owner = models.ForeignKey(User, on_delete=models.CASCADE)

    class meta:
        verbose_name_plural ='entries'

    def __str__(self):
        #返回模型的字符串表示
        if len(self.text) >= 50:
            return self.text[:50] + '...'
        else:
            return self.text[:]

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • weixin_43590893 CyberBatMan2077 2020-08-27 10:49
    1. 看不到错误提示.
    2. 可以先在shell里看看owner是否被正常设置了, 是不是忘记进行数据迁移
    3. 只要按照书上按部就班操作的话, topic设定了owner后, entry作为它的外键(下一级)也会被设置owner
    点赞 评论 复制链接分享

相关推荐