Using the Django memcache API for dynamically created models

So, I have a function that creates a dynamic model. I do it very much like AuditTrail (see the Django wiki).

Sample code here:

https://gist.github.com/0212845ae00891efe555

Is there any way to make a dynamically generated class legible? Ideally, is something crazy monkeypatch / hack?

+4
source share
2 answers

I am aware of a problem where pickle cannot store a generated or dynamic class. I solved this by binding my dynamic type to modules like this:

new_class = type(name, (models.Model,), attrs) mod = sys.modules[new_class.__module__] mod.__dict__[new_class.__name__] = new_class 

This is a FAR from a clean or elegant solution, so if anyone can think of a more convenient way for Django to do this, I’m all ears. However, the above code does work.

+1
source

The reason there are no answers is because the answer is most likely a hacker one. I do not think that you can decompose an object in Python without knowing the structure of the class on the receiving side without any hacker solution. The big reason for disassembling does not support this, probably because it is a fantastic way to inject malicious code into your application.

http://www.mofeel.net/871-comp-lang-python/2898.aspx explains why dynamically created classes cannot be scattered.

In each case, I either simply serialize the attribute dictionary of the object using the dict method, or just figure out some terrible work. I hope you come up with something better.

Good luck

0
source

Source: https://habr.com/ru/post/1311744/


All Articles