我正在编写一个库系统,我收到了这个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'
答案 0 :(得分:0)
错误消息告诉您具体操作:您需要将PurchaseOrderInfo分配给purchase_sn-Member。
您可以通过PurchaseOrderInfo.get
查找它,或者如果您将purchase_sn设置为另一个PurchaseOrderInfo的主键,它可以正常工作。这是一个整数,而不是request.POST
祝你Django努力!