27👍
In Django templates, the dot notation (testobj.test
) can resolve to the Python’s []
operator. This means that all you need is an ordinary dict:
testobj = {'test':[a1,a2,b2], 'test2':'something else', 'test3':1}
Pass it as testobj
variable to your template and you can freely use {{ testobj.test }}
and similar expressions inside your template. They will be translated to testobj['test']
. No dedicated class is needed here.
34👍
You can use built-in type function:
testobj = type('testclass', (object,),
{'test':[a1,a2,b2], 'test2':'something else', 'test3':1})()
But in this specific case (data object for Django templates), you should use @Xion’s solution.
- [Django]-(13: Permission denied) while connecting to upstream:[nginx]
- [Django]-Getting 'DatabaseOperations' object has no attribute 'geo_db_type' error when doing a syncdb
- [Django]-Django: How to save data to ManyToManyField?
29👍
There is another solution in Python 3.3+ types.SimpleNamespace
from types import SimpleNamespace
test_obj = SimpleNamespace(a=1, b=lambda: {'hello': 42})
test_obj.a
test_obj.b()
- [Django]-A left outer reverse select_related in Django?
- [Django]-How to simplify migrations in Django 1.7?
- [Django]-How do I prevent fixtures from conflicting with django post_save signal code?
- [Django]-What is the canonical way to find out if a Django model is saved to db?
- [Django]-Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools
- [Django]-Django: Create fixtures without specifying a primary key?
3👍
use building function type: document
>>> class X:
... a = 1
...
>>> X = type('X', (object,), dict(a=1))
first and second X are identical
- [Django]-Django: Best way to unit-test an abstract model
- [Django]-Django 1.4 timezone.now() vs datetime.datetime.now()
- [Django]-Django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet. (django 2.0.1)(Python 3.6)
2👍
Here’s a rogue, minimalist way to create an object. A class is an object, so just commandeer the class definition syntax as if it were a Python object literal:
class testobj(object):
test = [a1,a2,b2]
test2 = 'something else'
test3 = 1
Class variables are the members of the object, and are easily referenced:
assert testobj.test3 == 1
This is weird, a class never used as a class: it’s never instantiated. But it’s a low-clutter way to make an ad hoc, singleton object: The class itself is your object.
- [Django]-Model not showing up in django admin
- [Django]-Get the index of an element in a queryset
- [Django]-Composite primary key in django
2👍
if you just need a "QuickRecord" you can simply declare a empty class
and you can use it without having to instantiate an object…
(just seize the dynamic features of python language… "á la Javascript")
# create an empty class...
class c1:pass
# then just add/change fields at your will
c1.a = "a-field"
c1.b = 1
c1.b += 10
print( c1.a, " -> ", c1.b )
# this has even the 'benesse' of easealy taking a
# snapshot whenever you want
c2 = c1()
print( c2.a, " -> ", c2.b )
- [Django]-What is the best way to access stored procedures in Django's ORM
- [Django]-Django Test Client Method Override Header
- [Django]-TypeError while using django rest framework tutorial
1👍
for the sake of completeness, there is also recordclass
:
from recordclass import recordclass
Test = recordclass('Test', ['test', 'test1', 'test2'])
foo = Test(test=['a1','a2','b2'], test1='someting else', test2=1)
print(foo.test)
.. ['a1', 'a2', 'b2']
- [Django]-Django Overriding Model Clean() vs Save()
- [Django]-Composite primary key in django
- [Django]-Django Templates First element of a List
0👍
The code below also require a class to be created however it is shorter:
>>>d = {'test':['a1','a2','b2'], 'test2':'something else', 'test3':1}
>>> class Test(object):
... def __init__(self):
... self.__dict__.update(d)
>>> a = Test()
>>> a.test
['a1', 'a2', 'b2']
>>> a.test2
'something else'
- [Django]-How to force-save an "empty"/unchanged django admin inline?
- [Django]-Django – Circular model import issue
- [Django]-'staticfiles' is not a valid tag library: Template library staticfiles not found