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

时间:2022-12-08 00:44:22

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.

我在写旅行计划,我有用户。出于这个问题的目的,让我们假设我的模型就像拥有一个“Trip”模型和一个“UserProfile”模型一样简单。

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.

该站点有一个功能,允许搜索路由(通过外部api),然后动态地将它们组装成“trips”,然后显示出来。新的搜索会删除所有旧的“旅行”,并找出新的。

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.

我的问题是:我想将这些访问保存到用户配置文件中。如果用户选择旅行,我希望它与该概要文件永久关联。目前,我的用户配置文件中有许多用于trip的tomany字段,但是当trip被“清理/刷新”时,所有的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.

我正在寻找一种简单的方法来复制那个旅行数据,或者在我将它添加到一个概要文件后使它保持静态。我不知道从哪里开始。目前,它的配置方式是有一个trips_profile datatable,它具有“trips”表的外键……如果我们不总是删除/刷新trips表,那就好了。

Help appreciated.

帮助感激。

1 个解决方案

#1


2  

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.

它将只删除未分配给用户配置文件的Trips。

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:

第二种方法的好处是不需要对UserProfile进行任何更改,甚至不需要对UserProfile进行任何更改,因为您可以让用户使用以下方法:

some_user.trip_set.all()

#1


2  

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.

它将只删除未分配给用户配置文件的Trips。

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:

第二种方法的好处是不需要对UserProfile进行任何更改,甚至不需要对UserProfile进行任何更改,因为您可以让用户使用以下方法:

some_user.trip_set.all()