Just use this in place of any dictionary you need to be serializeable. using System.Collections.Generic; using UnityEngine; [System.Serializable] public class SerializableDictionary : Dictionary, ISerializationCallbackReceiver { [SerializeField] private List m_Keys = new List(); [SerializeField] private List m_Values = new List(); public void OnBeforeSerialize() { m_Keys.Clear(); m_Values.Clear(); using Enumerator enumerator = GetEnumerator(); while (enumerator.MoveNext()) { KeyValuePair current = enumerator.Current; m_Keys.Add(current.Key); m_Values.Add(current.Value); } } public void OnAfterDeserialize() { Clear(); for (int i = 0; i < m_Keys.Count; i++) { Add(m_Keys[i], m_Values[i]); } m_Keys.Clear(); m_Values.Clear(); } } Answer from foodeyemade on reddit.com
🌐
Unity
discussions.unity.com › questions & answers
[Solved]How to serialize Dictionary with Unity Serialization System - Questions & Answers - Unity Discussions
May 21, 2013 - Edit: http://forum.unity3d.com/threads/finally-a-serializable-dictionary-for-unity-extracted-from-system-collections-generic.335797/ Hello everyone, I already read many things about Serialization with Unity and i already know that Dictionary are not serialized by Unity.
🌐
Reddit
reddit.com › r/unity3d › why doesn't unity serialize dictionaries???
r/Unity3D on Reddit: Why doesn't Unity serialize Dictionaries???
October 29, 2024 -

Dictionaries are just better resilent arrays with a search complexity O(1). I was working with scriptable objects and found out the hard way that unity doesnt save variables in scriptable objects that cannot be serialized. Had to use Odin Inspector's custom scriptable object for a quick workaround.

I'm sure theres a good reason to not serialize dictionaries... but cant think of any reason not to.

Discussions

Dictionary serialization with UIElement? - Unity Engine - Unity Discussions
Is it possible to serialize a dictionary with UIElement in Unity editor? I have tried many Assets, but they all have various problems. I still tend to use the built-in features of Unity and as little code as possible to achieve this function. More on discussions.unity.com
🌐 discussions.unity.com
0
March 22, 2020
Why doesn't Unity try to make the Dictionary and Stack Serializable? - Unity Engine - Unity Discussions
Why doesn't Unity try to make the Dictionary and Stack Serializable · Because it wouldn’t make much sense. The generic Dictionary class can be used with all sorts of key types, not just strings. Even when you restrict the keys to only strings (which would be quite tricky or feels strange) ... More on discussions.unity.com
🌐 discussions.unity.com
1
May 8, 2021
[Released] Dictionaries are now supported !!!! :D - Unity Engine - Unity Discussions
😄Just kidding, I went through a lot of hard work and research to get to this stage, I am still testing it, nailing out bugs, and will upload to asset store soon. I’m going to solve Dictionary for Unity once and for all! The Unity Dictionary Paradox (Read this section if you are unfamiliar ... More on discussions.unity.com
🌐 discussions.unity.com
0
April 24, 2016
Serialize a Dictionary - Questions & Answers - Unity Discussions
According to the docs a Dictionary can’t be serialized in Unity so I was thinking about buying an asset that can do that so that i can save my dictionaries to file, but I’m able to serialize and deserialize a Dictionary using the BinaryFormatter, I’m not sure I understand, can someone ... More on discussions.unity.com
🌐 discussions.unity.com
0
October 7, 2018
🌐
Unity
discussions.unity.com › unity engine
Serialization Dictionary Generic - Unity Engine - Unity Discussions
September 21, 2010 - I know, I’ve red that Unity has a problem when deserializing Dictionary Generic so we can no use them. But this is not entire true, unity has a problem but only when serialized versions comes from 2 different builds version (you know Unity change the assembly in every build).
🌐
Unity
discussions.unity.com › unity engine
Dictionary serialization with UIElement? - Unity Engine - Unity Discussions
March 22, 2020 - Is it possible to serialize a dictionary with UIElement in Unity editor? I have tried many Assets, but they all have various problems. I still tend to use the built-in features of Unity and as little code as possible to…
🌐
Unity
discussions.unity.com › unity engine
Why doesn't Unity try to make the Dictionary and Stack Serializable? - Unity Engine - Unity Discussions
May 8, 2021 - Why doesn't Unity try to make the Dictionary and Stack Serializable · Because it wouldn’t make much sense. The generic Dictionary class can be used with all sorts of key types, not just strings. Even when you restrict the keys to only strings (which would be quite tricky or feels strange) ...
Find elsewhere
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › microsoft.mixedreality.toolkit.utilities.serializabledictionary-2
SerializableDictionary<TKey,TValue> Class (Microsoft.MixedReality.Toolkit.Utilities) | Microsoft Learn
[<System.Serializable>] type SerializableDictionary<'Key, 'Value> = class inherit Dictionary<'Key, 'Value> interface ISerializationCallbackReceiver · Public Class SerializableDictionary(Of TKey, TValue) Inherits Dictionary(Of TKey, TValue) Implements ISerializationCallbackReceiver
🌐
Unity
docs.unity3d.com › 2020.1 › Documentation › Manual › JSONSerialization.html
Unity - Manual: JSON Serialization
When you pass in an object to the standard Unity serializer for processing, the same rules and limitations apply as they do in the Inspector: Unity serializes fields only; and types like Dictionary<> are not supported.
🌐
GitHub
github.com › Sterberino › UnitySerializedDictionary
GitHub - Sterberino/UnitySerializedDictionary: A serializable Dictionary and property drawer for the Unity Editor. · GitHub
A serializable Dictionary and property drawer for the Unity Editor, modeled visually after the Odin serialized dictionary.
Author   Sterberino
🌐
Unity
discussions.unity.com › unity engine
[Released] Dictionaries are now supported !!!! :D - Unity Engine - Unity Discussions
April 24, 2016 - 😄Just kidding, I went through a lot of hard work and research to get to this stage, I am still testing it, nailing out bugs, and will upload to asset store soon. I’m going to solve Dictionary for Unity once and for all! The Unity Dictionary Paradox (Read this section if you are unfamiliar ...
🌐
Unity Asset Store
assetstore.unity.com › home › tools › inspectionary - serializeable dictionary
Inspectionary - Serializeable Dictionary | Tools | Unity Asset Store
Inspectionary - Serializeable Dictionary
Get the Inspectionary - Serializeable Dictionary package from Square Pie Studios and speed up your game development process. Find this & other Tools options on the Unity Asset Store.
Price   $4.99
🌐
Unity
discussions.unity.com › questions & answers
Serialize a Dictionary - Questions & Answers - Unity Discussions
October 7, 2018 - According to the docs a Dictionary can’t be serialized in Unity so I was thinking about buying an asset that can do that so that i can save my dictionaries to file, but I’m able to serialize and deserialize a Dictionary …
🌐
Unity
docs.unity3d.com › 6000.1 › Documentation › ScriptReference › SerializeReference.html
Unity - Scripting API: SerializeReference
Must not derive from UnityEngine.Object. For example it cannot be a GameObject, MonoBehaviour, ScriptableObject or Transform. Must not be a C# Value type. Therefore simple types like integers, as well as structures are not supported, and should be serialized without the [SerializeReference] attribute instead. Must not be a C# Dictionary, or other type that is not supported by Unity serialization
🌐
Unity
discussions.unity.com › unity engine
Finally, a serializable dictionary for Unity! (extracted from System.Collections.Generic) - Unity Engine - Unity Discussions
June 24, 2015 - Hey guys, so as all you all know Unity doesn’t know how to serialize generic dictionaries. Since 4.6 we got the ISerializationCallbackReceiver which allows us to use custom serialization to serialize types that Unity ca…
Top answer
1 of 2
6

The root problem here is that you don't understand why you are getting the error

name 'publisher__stats__elapsed_time_in_seconds' is not defined`.

Let's look at the code again:

Article.objects.filter(created_on__lte=datetime.now(timezone.utc) -
    timedelta(hours=0, minutes=0, seconds=publisher__stats__elapsed_time_in_seconds))

In the code above the symbol publisher__stats__elapsed_time_in_seconds is interpreted as a variable reference, which is not defined in your code. If you added

publisher__stats__elapsed_time_in_seconds = 1

just before the code snippet above, then you would not get the error. (You would also not get the results you want.) You're expecting Django's ORM to operate on the symbol publisher__stats__elapsed_time_in_seconds but before Django can get to it, the Python interpreter must interpret the code, and the way the code is written, that's just a variable name which the interpreter must resolve. Django does not get a chance to even see it.

Ok so the way to prevent the interpreter to interpret the name as a variable reference, and have Django ORM's process the name, is to use F() expressions. So you'd be tempted to just do this:

Article.objects.filter(created_on__lte=datetime.now(timezone.utc) -
    timedelta(hours=0, minutes=0, seconds=F("publisher__stats__elapsed_time_in_seconds")))

But then you'd be passing to timedelta a parameter it does not know how to handle.

As schillingt pointed out in a comment, an answer elsewhere by Lutz Prechelt shows how to move the F() expression outside the timedelta. In your case, you could do this:

Article.objects.filter(created_on__lte=datetime.now(timezone.utc) -
        timedelta(seconds=1) * F("publisher__stats__elapsed_time_in_seconds"))))
2 of 2
0

Based on your publisher model, there is a problem on how you are defining the field to compare with. Your field in the question is

publisher__stats__elapsed_time_in_seconds

Your query code is

Article.objects.filter(created_on__lte=datetime.now(timezone.utc) - timedelta(hours=0, minutes=0, seconds=publisher__stats__elapsed_time_in_seconds)).

This basically means get me all articles that have a creation time(created_on field) before or exactly equal to the time(elapsed_time_in_seconds field) for the article-->publisher-->stats record.

This means it looks for a elapsed_time_in_seconds field on the stats object which should be related to your publisher object via a FK or some other way. The publisher object in turn needs to be related to your article object.

Your Article model is related to the Publisher model, but there does not seem to be a relation between the Publisher and the Stats (??). Can you check the stats model to see that the field and the relation is defined correctly?

🌐
Unity
discussions.unity.com › unity engine
How to serialize Dictionary<string, string> and Dictionary<string, CustomClass[]> in MessagePack? - Unity Engine - Unity Discussions
May 10, 2021 - So I seem to have hit this roadblock in building my app where an attempt to serialize these two objects of called “Dictionary stat” and “Dictionary properties” just seem impossible. my code is sequential of course and an exception is returned at the point of serializing “stat” which comes before the serialization of"properties", meaning the code never even reaches the point of serializing properties dictionary, thus my first challenge is getting past serializ...
🌐
GitHub
github.com › audunegames › serializable-dictionary
GitHub - audunegames/serializable-dictionary: Serializable version of a C# dictionary for use in Unity.
A SerializableDictionary<TKey, TValue> class to use in place of the C# Dictionary<TKey, TValue> class, but with the benefit of the data being serialized in Unity.
Author   audunegames