繁体   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