0

模型.py

class Author(models.Model):
    author_id=models.AutoField(primary_key=True)
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=40)
    email = models.EmailField()
    age=models.IntegerField()

    class Meta:
    db_table=u'Author Info'

    def __unicode__(self):
        return u"%d %s %s %s %d" % (self.pk, self.first_name, self.last_name, self.email,self.age)

    def books(self):
        return Book.objects.filter(author=self)

class Book(models.Model):
    book_id=models.AutoField(primary_key=True,unique=True)
    book_name=models.CharField(max_length=30)
    publisher_name=models.CharField(max_length=40)
    author=models.ForeignKey(Author)

    class Meta:
        db_table = u'Book Name'

    def __unicode__(self):
        return u'%d %s %s' % (self.pk, self.book_name, self.publisher_name)

视图.py

def addbook(request):
    if request.POST:
        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(first_name = first_name,last_name = last_name,email=email,age=age)
        author=author.save()
        book_name = request.POST.get('book_name')
        publisher_name = request.POST.get('publisher_name')
    #Book.author_id=author
        #author_id = author_id()
        book=Book.objects.create(book_name=book_name,publisher_name=publisher_name,author_id)
    book.save()
    return redirect('/index/')
    else:
        return render_to_response('addbook.html',context_instance=RequestContext(request))

index.html/模板

table border="0" cellpadding='8' cellspacing='10'>
    <tr>
        <td align="right" colspan="8"><a href="/addbook/">Add Book</a></td>
    </tr>

    <tr>
        <th>Book Id</>
    <th>Book name</th>
    <th>Publication name</th>
    <th>Author Id</th>
    <th>First Name</th>
    <th>Last Name</th>
    <th>E Mail</th>
    <th>Age</th>
    </tr>
    {% for book in books %}
    <tr>
       <td>{{ book.book_id }}</td>
       <td>{{ book.book_name }}</td>
       <td>{{ book.publisher_name }}</td>
       <td>{{ book.author_id }}</td>

       {% for author in authors %}  
         <td>{{author.first_name}} </td><td>{{author.last_name}}</td>
         <td>{{author.email}}</td>
         <td>{{author.age}}</td>
           {% endfor %}
             <td><a href="/editbook/{{ book.book_id}}">Edit</a></td>
             <td><a href="/deletebook/{{ book.book_id}}">Delete</a></td>
        {% endfor %}

在这里如何在 Book 类中为“作者”或 author_id 字段分配外键值,请给出分配程序。我无法将值分配给外键字段...如何分配或是否有任何其他可用功能请解释我

4

2 回答 2

2

问题是这样的:

author = Author(first_name = first_name,last_name = last_name,email=email,age=age)
author=author.save()

save()实际上并没有返回任何东西,即使我相信它应该。将其更改为简单的:

author = Author(first_name = first_name,last_name = last_name,email=email,age=age)
author.save()
book.author = author
book.save()

现在你有了你的作者,你可以分配给book.author.

另外,当我在这里时,您应该了解反向关系。不需要以下方法:

def books(self):
    return Book.objects.filter(author=self)

因为你可以这样做:

author.book_set.all()
于 2013-03-04T11:17:51.737 回答
1

完整性错误是由于将空值传递给 Book 表的 author_id 列。检查这一行book=Book.objects.create(book_name=book_name,publisher_name=publisher_name,author_id)

如果你想在书表中插入相同的作者 ID,你可以使用

django 信号

或者您必须通过查询一些唯一参数(例如电子邮件 ID 值)从 Author 表中获取最新的作者 ID,并将此 ID 传递给 Books 表

于 2013-03-04T10:04:41.377 回答