簡體   English   中英

我正在編寫一個圖書館系統,並且收到此Django錯誤消息

[英]I'm writing a library system, and I'm getting this Django Error Message

我正在編寫一個圖書館系統,並且收到此Django錯誤消息:

無法分配“'1'”:“ BookInfo.purchase_sn”必須是“ PurchaseOrderInfo”實例。

並且還會在author_sn,author_type ....和所有foreginkey字段上顯示相同的消息。

以下是我的觀點和模型。

我究竟做錯了什么?

my view is :

def book_add_new(request):
    print(request.POST)
    book_add_info = BookInfo(book_sn=request.POST['this_bk_sn'],
                             voucher_sn=int(request.POST['vochr_sn']),
                             book_genre=request.POST['bk_genre'],
                             book_state=request.POST['bk_state'],
                             book_group_sn=request.POST['cpy_group'],
                             book_classification=request.POST['bk_classification'],
                             book_primary_title=request.POST['primry_title'],
                             book_secondary_title=request.POST['scndry_title'],
                             author_sn=request.POST['authr_sn'],
                             author_type=request.POST['authr_type_sn'],
                             publisher_sn=request.POST['puplshr_sn'],
                             publishing_country=request.POST['cntry_sn'],
                             book_price=request.POST['bk_price'],
                             book_isbn=request.POST['bk_isbn'],
                             book_edition=request.POST['bk_edition'],
                             publish_date=request.POST['bk_year'],
                             book_page_count=request.POST['page_count'],
                             purchase_sn=request.POST['prchs_sn'],
                             book_in=1)
    book_add_info.save()
    return render(request, 'book_management/book_add_new_page.html')

我的模特:

class PurchaseOrderInfo(models.Model):
    purchase_sn = models.IntegerField(primary_key=True)
    purchase_owner = models.TextField(blank=True, null=True)
    order_date = models.DateField(blank=True, null=True)
    order_scan = models.BinaryField(blank=True, null=True)

    class Meta:
        managed = True
        db_table = 'purchase_order_info'


class BookInfo(models.Model):
    book_classification = models.TextField(blank=True, null=True)
    book_isbn = models.TextField(blank=True, null=True)
    book_secondary_title = models.TextField(blank=True, null=True)
    book_group_sn = models.IntegerField(blank=True, null=True)
    voucher_snv = models.ForeignKey(BookVoucher, models.DO_NOTHING, db_column='voucher_sn', blank=True, null=True)
    book_primary_title = models.TextField(blank=True, null=True)
    purchase_sn = models.ForeignKey(PurchaseOrderInfo, models.DO_NOTHING, db_column='purchase_sn', blank=True,
                                    null=True)
    book_page_count = models.IntegerField(blank=True, null=True)
    book_sn = models.IntegerField(primary_key=True)
    book_genre = models.ForeignKey(BookGenres, models.DO_NOTHING, db_column='book_genre', blank=True, null=True)
    book_state = models.ForeignKey('BookState', models.DO_NOTHING, db_column='book_state', blank=True, null=True)
    book_edition = models.IntegerField(blank=True, null=True)
    publish_date = models.IntegerField(blank=True, null=True)
    book_price = models.TextField(blank=True, null=True)  # This field type is a guess.
    author_sn = models.ForeignKey(AuthorInfo, models.DO_NOTHING, db_column='author_sn', blank=True, null=True)
    author_type = models.ForeignKey(AuthorType, models.DO_NOTHING, db_column='author_type', blank=True, null=True)
    publisher_sn = models.ForeignKey(PublisherInfo, models.DO_NOTHING, db_column='publisher_sn', blank=True, null=True)
    book_in = models.BooleanField()
    publishing_country = models.ForeignKey('CountryInfo', models.DO_NOTHING, db_column='publishing_country', blank=True,
                                           null=True)

    def __str__(self):
        return self.book_primary_title

    class Meta:
        managed = True
        db_table = 'book_info'

錯誤消息將告訴您確切的操作:您需要將PurchaseOrderInfo分配給purchase_sn-Member。

您可以通過PurchaseOrderInfo.get查找它,或者如果將purchase_sn設置為另一個PurchaseOrderInfo的主鍵,則可以正常工作。 這是一個整數,而不是request.POST的字符串

祝您在Django上的努力好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM