Django 模型表临时数据与永久数据

发布于 2024-12-29 18:44:58 字数 464 浏览 3 评论 0原文

我正在写一个旅行计划,并且我有用户。出于此问题的目的,让我们假设我的模型就像拥有“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明天过后 2025-01-05 18:44:58

如果没有模型,很难准确地说,但考虑到以下布局:

class UserProfile(models.Model):
    trips = models.ManyToManyField(Trip)

您可以通过执行以下操作来清除无用的 Trip

Trip.objects.filter(userprofile__isnull=True).delete()

这只会删除未分配给 TripTrip代码>用户配置文件。

但是,考虑到以下布局:

class Trip(models.Model):
    users = models.ManyToManyField(User)

您可以使用以下方法杀死无用的行程:

Trip.objects.filter(users__isnull=True).delete()

第二种方法的副作用是不需要对 UserProfile 甚至 UserProfile 进行任何更改,因为您可以通过以下方式获取用户的行程:

some_user.trip_set.all()

It's hard to say exactly without your models, but given the following layout:

class UserProfile(models.Model):
    trips = models.ManyToManyField(Trip)

You can clear out useless Trips by doing:

Trip.objects.filter(userprofile__isnull=True).delete()

Which will only delete Trips not assigned to a UserProfile.

However, given the following layout:

class Trip(models.Model):
    users = models.ManyToManyField(User)

You could kill the useless trips with:

Trip.objects.filter(users__isnull=True).delete()

The second method has the side benefit of not requiring any changes to UserProfile or even a UserProfile at all, since you can then just get a Users trips with:

some_user.trip_set.all()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文