gpt4 book ai didi

Python Django 多数据库提交具有外键关系的对象

转载 作者:行者123 更新时间:2023-12-01 05:31:12 24 4
gpt4 key购买 nike

我正在将 Django 与多个数据库一起使用。我有一个“预览”数据库,它接收用户上传的消息,这些消息必须由管理员预览并“接受”,此时它们将被提交到“默认”生产数据库。以下 View 应该可以做到这一点,但我收到错误。每个 newSentenceModel 都有一个指向每个 newMessageSegment 的外键,每个 newMessageSegment 都有一个指向每条消息的外键。如果管理员接受内容,我想将每个项目移动到新数据库,然后删除预览数据库中的旧条目。请帮忙!谢谢-

这是错误:

instance is on database "preview", value is on database "default"

错误消息出现在这一行:

newMessageSegment.msg = newMessage # Setup the foreign key to the msg itself

这是查看功能:

## This view is used when the admin approves content and clicks on the "accept content" button when reviewing 
## a recent upload - it saves the data to the production database
def accept_content(request, msg_id=None):
if msg_id == None: # If for some reason we got a None, then it's not a valid page to accept so redirect home
return HttpResponseRedirect("/") # Redirect home
msgList = Message.objects.using('preview').all() # Get all Messages
msgSegmentList = MessageSegment.objects.using('preview').all() # Get all MessageSegment Objects
sentenceModels = SentenceModel.objects.using('preview').all() # Get all SentenceModels
for msgs in msgList: # Iterate all msgs
if int(msgs.id) != int(msg_id): # Don't care if it is not the msg needing review
continue # Short Circuit
msgPrimaryKey = msgs.pk # Extract the primary key from this msg to restore later
msgs.pk = None # Erase the primary key so we can migrate databases properly
newMessage = msgs # This is the msg to transfer to the new one
newMessage.save(using='default') # Save the item to the production database
for msgSegment in msgSegmentList: # Iterate all msg segments for this msg
if msgSegment.msg_id == msgPrimaryKey: # Check the foreign keys on the msg segment to msg connection
newMessageSegment = msgSegment # Define a new msg segment
msgSegment.pk = None # Erase the primary key so we can assign it properly
newMessageSegment.pk = None # Erase the primary key so we can assign it properly
newMessageSegment.msg = newMessage # Setup the foreign key to the msg itself
newMessageSegment.save(using='default') # Save the item to the production database
for sentenceModel in sentenceModels: # Iterate all sentences for this msg segment
if sentenceModel.msg_segment_id == msgSegment.id: # Determine which sentences are for this msg segment
newSentenceModel = sentenceModel # Define the newSentenceModel
newSentenceModel.msg_segment = newMessageSegment # Setup the foreign key to the msg segment
newSentenceModel.save(using='default') # Save the item to the production database
sentenceModel.delete(using='preview') # Delete the item from the review database
msgSegment.delete(using='preview') # Delete the item from the review database
msgs.pk = msgPrimaryKey # Restore the key so we can delete it properly
msgs.delete(using='preview') # Delete the item from the review database
return HttpResponseRedirect("/")

最佳答案

Django 会记住保存对象的数据库,因此每个 newMessageSegment 仍然附属于 preview 数据库,直到您将其保存到 default并且它正确地禁止跨数据库 FK 分配。这尚未经过测试,但分配给底层 msg_id 字段可能会起作用:

newMessageSegment.msg_id = newMessage.id

如果失败,您可以创建 newMessageSegment 的新副本,而不仅仅是创建对其的新引用。我认为您可以通过迭代 msgSegment._meta.fields 来实现自动化,但我可能会忽略继承之类的微妙之处。任何多对多字段都会很痛苦。

或者,如果您只是想破解它,请编辑跟踪它的内部对象。我通常不建议这样做,但无论如何,当您保存时它都会改变。

newMessageSegment._state.db = "default"
newMessageSegment.msg = newMessage
newMessageSegment.save(using="default")

关于Python Django 多数据库提交具有外键关系的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20255893/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com