我正在使用Django和多个数据库。我有一个“预览”数据库,它接收用户上传的消息,这些数据必须由管理员预览并“接受”,此时他们将被提交到“默认”生产数据库。以下视图应该这样做,但我收到一个错误。每个newSentenceModel都有一个指向每个newMessageSegment的外键,每个newMessageSegment都有一个指向每个Message的外键。如果管理员接受内容,我想将每个项目移动到新数据库,然后删除预览数据库中的旧条目。请帮忙!谢谢 -
这是错误:
instance is on database "preview", value is on database "default"
此行出现错误消息:
newMessageSegment.msg = newMessage # Setup the foreign key to the msg itself
这是View功能:
## 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("/")
答案 0 :(得分:2)
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")