JSONField() ordering and removing latest

65 views
Skip to first unread message

wagner

unread,
Jan 30, 2020, 10:38:33 AM1/30/20
to Django users
Hello together,
i have the following problem. I need to save different versions of an article in a json object:

{
"1580388458.2877874": {
"title": "Erster Artikel", 
"text_block_with_image": [{"text": "Super Text der einen weiterhilft", "image": "http://test.de/test.jpg"}]
}, 

"1580388556.6462297": {
"title": "Erster Artikel angepasst", 
"text_block_with_image": [{"text": "Super neuer neuer Text der einen weiterhilft", "image": "http://test.de/test.jpg"}]
}

}

I save this information in a JSONField().

The numbers here are the current time in seconds. 
I want to do an article template where i show the latest version of the article (by current time seconds).

How can i filter after the latest version?

Articles.objects.filter( ?? )



My second problem:

How can i delete the oldest version (key), when i add a new entry?


Thank you!

Jody Fitzpatrick

unread,
Feb 4, 2020, 8:03:34 PM2/4/20
to Django users
I would change the model a bit, instead of having the timestamp as the key, i would set it in the dict.
I believe that would be cleaner and would allow you to do a bit more of what you are going for.

Andrew C.

unread,
Feb 4, 2020, 10:00:11 PM2/4/20
to django...@googlegroups.com
Usually, something that has multiple versions could be in an autoincrement field as the primary key. Then, in a JSONB field with GIN index (since you’re using PostgreSQL), you would have a dict of each version, like this:

{
“1”: { # beginning of article 1
“versions”: {

“1”: {
“title”: “Blah1”,
“timestamp”: 172903740.0002782,
“image”: “http:test.de/test.jpg”,
“alt”: “Super text”
}, # end of version 1
“2”: {
“title”: “Blah1”,
“timestamp”: 1729375394.0002782,
“image”: “http:test.de/test.jpg”,
“alt”: “Super text blah”
}, # end of version 2
“3”: {
“title”: “Blah2”,
“timestamp”: 17290826300.0002782,
“image”: “http:test.de/test.jpg”,
“alt”: “Super text blah”
} # end of version 3 

} # end of versions attribute/column
}, # end of article 1
“2”: {


“versions”: {

“1”: {
“title”: “Blah1”,
“timestamp”: 172903740.0002782,
“image”: “http:test.de/test.jpg”,
“alt”: “Super text”
}, # end of version 1
“2”: {
“title”: “Blah1”,
“timestamp”: 172903740.0002782,
“image”: “http:test.de/test.jpg”,
“alt”: “Super text blah”
} # end of version 2

} # end of article 2

} # end of queryset

So your primary key is a BigAutoField and your “versions” attribute/column is a JSONField. IF YOU ARE FILTERING BASED ON ARTICLE TITLE, make sure you put a GIN Index on that JSONField.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f0624188-fd77-4c50-acbe-b0c841043554%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages