如何在跟踪历史的同时让每个人都可以编辑某些内容?

时间:2021-11-12 23:04:09

I want to add some wiki like feature to my already existing project.

我想在我现有的项目中添加一些类似wiki的功能。

The only reasonable example I found suggested to just add a "is_published" boolean field and have multiple versions with the same slug where only one version of each slug is ever published.

我发现的唯一合理的例子建议只添加一个“is_published”布尔字段,并且有多个版本具有相同的slug,其中每个slug只发布一个版本。

Now I find some trouble with that idea, because I need slug and ID to be unique and unchanging for every object, because I use those attributes in the url.

现在我发现这个想法有些麻烦,因为我需要slug和ID对每个对象都是唯一且不变的,因为我在url中使用了这些属性。

So I thought about doing something like this.

所以我想做这样的事情。

Imagine I have a model:

想象一下,我有一个模型:

class Post(models.Model):
  user = models.ForeignKey('auth.User')
  slug = models.SlugField()
  subject = models.CharField(max_length=255)
  body = models.TextField()

Now to get my feature I try this:

现在,为了得到我的功能,我试试这个:

class Post(models.Model):
  user = models.ForeignKey('auth.User')
  slug = models.SlugField()
  cv = models.ForeignKey('myapp.Version') // current version

class Version(models.Model):
  user = models.ForeignKey('auth.User') // this is the editor
  subject = models.CharField(max_length=255)
  body = models.TextField()
  post = models.ForeignKey('myapp.Post', related_name="all_versions")

This would allow me to use the Post object like before with minimal changes. I only need to add "cv" when I want to access subject or body. It shouldn't use additional resources when used with select_related.

这将允许我像以前一样使用Post对象进行最小的更改。当我想访问主题或正文时,我只需要添加“cv”。与select_related一起使用时,不应使用其他资源。

Is there a better way to do this?

有一个更好的方法吗?

Is there even a way that removes the need of inserting "cv" ?

有没有一种方法可以消除插入“cv”的需要?

2 个解决方案

#1


1  

There is an easy way to do it but you'll have to drop the uniqueness of the SlugField.

有一种简单的方法可以做到这一点,但你必须放弃SlugField的独特性。

class Post(models.Model):
    user = models.ForeignKey('auth.User')
    slug = models.SlugField(unique=False)
    subject = models.CharField(max_length=255)
    body = models.TextField()
    version = models.DateTimeField(auto_now_add=True, auto_now=True)

What you do in your view is simply pull the instances with the same slug and order them by date, the latest date will obviously be the current version.

您在视图中执行的操作只是使用相同的段塞拉实例并按日期排序,最新日期显然是当前版本。

Then in your template, you'll use the id field as a get variable, the redirect url for a version should look like this:
http://someurl.com/this-unique-post?v=3

然后在您的模板中,您将使用id字段作为get变量,版本的重定向URL应如下所示:http://someurl.com/this-unique-post?v = 3

Where 3 is the id of that post's version, then if you want to make, say the version with id=3 as the current version, you'll just save/update it again!

其中3是该帖子版本的id,那么如果你想制作,比如id = 3的版本作为当前版本,你只需要再次保存/更新它!

Hope this helps!

希望这可以帮助!

#2


0  

There are django apps for that. for example: https://github.com/etianen/django-reversion

有django应用程序。例如:https://github.com/etianen/django-reversion

#1


1  

There is an easy way to do it but you'll have to drop the uniqueness of the SlugField.

有一种简单的方法可以做到这一点,但你必须放弃SlugField的独特性。

class Post(models.Model):
    user = models.ForeignKey('auth.User')
    slug = models.SlugField(unique=False)
    subject = models.CharField(max_length=255)
    body = models.TextField()
    version = models.DateTimeField(auto_now_add=True, auto_now=True)

What you do in your view is simply pull the instances with the same slug and order them by date, the latest date will obviously be the current version.

您在视图中执行的操作只是使用相同的段塞拉实例并按日期排序,最新日期显然是当前版本。

Then in your template, you'll use the id field as a get variable, the redirect url for a version should look like this:
http://someurl.com/this-unique-post?v=3

然后在您的模板中,您将使用id字段作为get变量,版本的重定向URL应如下所示:http://someurl.com/this-unique-post?v = 3

Where 3 is the id of that post's version, then if you want to make, say the version with id=3 as the current version, you'll just save/update it again!

其中3是该帖子版本的id,那么如果你想制作,比如id = 3的版本作为当前版本,你只需要再次保存/更新它!

Hope this helps!

希望这可以帮助!

#2


0  

There are django apps for that. for example: https://github.com/etianen/django-reversion

有django应用程序。例如:https://github.com/etianen/django-reversion