Suppose I have got a model ArticleVersion in my project which is defined as:
class ArticleVersion(models.Model):
article = models.ForeignKey(Article)
version = models.PositiveIntegerField()
content = models.TextField()
created = models.DateTimeField(auto_now_add=True)
media_type = models.ForeignKey(MediaType)
author = models.ForeignKey(User, null=True)
When the user fills in the form for this model I check whether the content of version from the form is different and if it is I create a new version. Actually now it looks something like this
if self.old_content != updated_version.content:
# create new version if content is different
updated_version = self.article.articleversion_set.create(
article=self.article,
version=self.article.latest_version.version + 1,
content=updated_version.content,
media_type=updated_version.media_type,
author=updated_version.author
)
But if I put more fields in the model it would be too verbose. I was wondering is there a way to do such things in Django more concisely?
You can just set
article.pktoNone, and when you save a new entry will be created.