模型.py 是
class Book(models.Model):
book_name=models.CharField(max_length=30)
author_name=models.CharField(max_length=30)
publisher_name=models.CharField(max_length=40)
author=models.ForeignKey(Author)
def __unicode__(self):
..........
class Author(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=40)
email = models.EmailField()
age=models.IntegerField()
def __unicode__(self):
........
def books(self):
return Book.objects.filter(author=self)
我需要执行编辑并将已经存在的数据保存在数据库中的表中。我正在使用 2 个模型。因为我用单个表做同样的事情,所以对两个表执行有些困惑。我是 django 的学习者。请帮我解决这个问题
请检查我的 views.py 以获取编辑选项。
def editbook(request,book_id):
if request.POST:
book_name =request.POST['book_name']
publisher_name =request.POST['publisher_name']
books=Book.objects.filter(book_id=book_id).update(book_name=book_name, publisher_name=publisher_name)
first_name = request.POST.get('first_name')
last_name = request.POST.get('last_name')
email = request.POST.get('email')
age = request.POST.get('age')
author = Author.objects.update(first_name = first_name,last_name = last_name,email=email,age=age)
return redirect('/index/')
else:
books = Book.objects.get(pk=book_id)
return render_to_response('editbook.html',{'books':books},context_instance=RequestContext(request))
ya.this 无法正常工作。请指导我如何执行该操作。