Django 模型表临时数据与永久数据
我正在写一个旅行计划,并且我有用户。出于此问题的目的,让我们假设我的模型就像拥有“Trip”模型和“UserProfile”模型一样简单。
该网站有一项功能,允许搜索路线(通过外部 API),然后动态地将这些组合成“行程”,然后我们将其显示。新的搜索会删除所有旧的“行程”并找出新的“行程”。
我的问题是:我想将其中一些旅行保存到用户配置文件中。如果用户选择一次旅行,我希望它与该个人资料永久关联。目前,我的用户配置文件中有一个用于行程的多对多字段,但是当行程被“清理/刷新”时,所有行程都会被删除,并且该关联是无用的。我需要用户能够在一个月后返回并查看那次旅行。
我正在寻找一种简单的方法来复制该旅行数据,或者将其添加到配置文件后使其静态。 ..我不太知道从哪里开始。目前,它的配置方式是有一个 trips_profile 数据表,该数据表具有“trips”表的外键。 。 。如果我们不一直删除/刷新 Trips 表,那就没问题了。
帮助表示赞赏。
I am writing a trip planner, and I have users. For the purposes of this question, lets assume my models are as simple as having a "Trip" model and having a "UserProfile" model.
There is a functionality of the site that allows to search for routes (via external APIs), and then dynamically assembles those into "trips", which we then display. A new search deletes all the old "trips" and figures out new ones.
My problem is this: I want to save some of these trips to the user profile. If the user selects a trip, I want it to be permanently associated with that profile. Currently I have a ManyToMany field for Trips in my UserProfile, but when the trips are "cleaned/flushed", all trips are deleted, and that association is useless. I need a user to be able to go back a month later and see that trip.
I'm looking for an easy way to duplicate that trip data, or make it static once I add it to a profile . .. I don't quite know where to start. Currently, the way it is configured is there is a trips_profile datatable that has a foreign key to the "trips" table . . . which would be fine if we weren't deleting/flushing the trips table all the time.
Help appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果没有模型,很难准确地说,但考虑到以下布局:
您可以通过执行以下操作来清除无用的
Trip
:这只会删除未分配给
Trip
的Trip
代码>用户配置文件。但是,考虑到以下布局:
您可以使用以下方法杀死无用的行程:
第二种方法的副作用是不需要对
UserProfile
甚至UserProfile
进行任何更改,因为您可以通过以下方式获取用户
的行程:It's hard to say exactly without your models, but given the following layout:
You can clear out useless
Trip
s by doing:Which will only delete
Trip
s not assigned to aUserProfile
.However, given the following layout:
You could kill the useless trips with:
The second method has the side benefit of not requiring any changes to
UserProfile
or even aUserProfile
at all, since you can then just get aUser
s trips with: