Groups&THREE.MultiMaterial(Groups & THREE.MultiMaterial)
作为原型化一些调试器对象和方法的一部分,我创建了一个在每一侧都有不同颜色的简单立方体。 我决定使用绘图组和THREE.Multimaterial,而不是创建多个网格。 不幸的是,它只是半工作,我不明白为什么。
工作的是前两个边(4个三角形)。 什么是不起作用的是任何其他方面。 其他方面都不可见。 我添加了调试代码来绘制(基于索引的)顶点法线,(基于索引,手动计算)面法线和线框(THREE.WireframeHelper)。
如果我将第二组的计数增加到9,则没有任何反应。 如果我更改组以引用前8个顶点中的不同开始/计数值,则这些更改将按预期工作。 在第8个顶点以上做任何事都没有效果。
我还检查了drawRange设置为从0绘制到无穷大。 我还排除了数据中的拼写错误,因为否则法线和线框不起作用。 还有什么我想念的吗? 谢谢!
(我在r76中发现了这个问题。下面的代码在撰写本文时引用了r79。)
jsfiddle: http : //jsfiddle.net/TheJim01/gumftkm4/
HTML:
<script src="http://threejs.org/build/three.js"></script> <script src="http://threejs.org/examples/js/controls/TrackballControls.js"></script> <script src="http://threejs.org/examples/js/libs/stats.min.js"></script> <div id="host"></div> <script> // INITIALIZE var WIDTH = window.innerWidth, HEIGHT = window.innerHeight, FOV = 35, NEAR = 1, FAR = 1000; var renderer = new THREE.WebGLRenderer({ antialias: true }); renderer.setSize(WIDTH, HEIGHT); document.getElementById('host').appendChild(renderer.domElement); var stats= new Stats(); stats.domElement.style.position = 'absolute'; stats.domElement.style.top = '0'; document.body.appendChild(stats.domElement); var camera = new THREE.PerspectiveCamera(FOV, WIDTH / HEIGHT, NEAR, FAR); camera.position.z = 250; var trackballControl = new THREE.TrackballControls(camera, renderer.domElement); trackballControl.rotateSpeed = 5.0; // need to speed it up a little var scene = new THREE.Scene(); var light = new THREE.PointLight(0xffffff, 1, Infinity); light.position.copy(camera.position); scene.add(light); function draw(){ light.position.copy(camera.position); renderer.render(scene, camera); stats.update(); } trackballControl.addEventListener('change', draw); function navStartHandler(e) { renderer.domElement.addEventListener('mousemove', navMoveHandler); renderer.domElement.addEventListener('mouseup', navEndHandler); } function navMoveHandler(e) { trackballControl.update(); } function navEndHandler(e) { renderer.domElement.removeEventListener('mousemove', navMoveHandler); renderer.domElement.removeEventListener('mouseup', navEndHandler); } renderer.domElement.addEventListener('mousedown', navStartHandler); renderer.domElement.addEventListener('mousewheel', navMoveHandler); </script>
CSS:
html *{ padding: 0; margin: 0; width: 100%; overflow: hidden; } #host { width: 100%; height: 100%; }
JavaScript的:
// New Color Cube (function () { var pos = new Float32Array([ // front -1, 1, 1, -1, -1, 1, 1, 1, 1, 1, -1, 1, // right 1, 1, 1, 1, -1, 1, 1, 1, -1, 1, -1, -1, // back 1, 1, -1, 1, -1, -1, -1, 1, -1, -1, -1, -1, // left -1, 1, -1, -1, -1, -1, -1, 1, 1, -1, -1, 1, // top -1, 1, -1, -1, 1, 1, 1, 1, -1, 1, 1, 1, // bottom -1, -1, 1, -1, -1, -1, 1, -1, 1, 1, -1, -1 ]), nor = new Float32Array([ // front 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, // right 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, // back 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, // left -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, // top 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, // bottom 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0 ]), idx = new Uint32Array([ // front 0, 1, 2, 3, 2, 1, // right 4, 5, 6, 7, 6, 5, // back 8, 9, 10, 11, 10, 9, // left 12, 13, 14, 15, 14, 13, // top 16, 17, 18, 19, 18, 17, // bottom 20, 21, 22, 23, 22, 21 ]); var sideColors = new THREE.MultiMaterial([ new THREE.MeshLambertMaterial({ color: 'red' }), // front new THREE.MeshLambertMaterial({ color: 'green' }), // right new THREE.MeshLambertMaterial({ color: 'orange' }), // back new THREE.MeshLambertMaterial({ color: 'blue' }), // left new THREE.MeshLambertMaterial({ color: 'white' }), // top new THREE.MeshLambertMaterial({ color: 'yellow' }) // bottom ]); var cubeGeometry = new THREE.BufferGeometry(); cubeGeometry.addAttribute("position", new THREE.BufferAttribute(pos, 3)); cubeGeometry.addAttribute("normal", new THREE.BufferAttribute(nor, 3)); cubeGeometry.setIndex(new THREE.BufferAttribute(idx, 3)); cubeGeometry.clearGroups(); cubeGeometry.addGroup(0, 6, 0); cubeGeometry.addGroup(6, 6, 1); cubeGeometry.addGroup(12, 6, 2); cubeGeometry.addGroup(18, 6, 3); cubeGeometry.addGroup(24, 6, 4); cubeGeometry.addGroup(30, 6, 5); THREE.ColorCube = function (scaleX, scaleY, scaleZ) { THREE.Mesh.call(this, cubeGeometry, sideColors); var scaler = new THREE.Matrix4().makeScale(scaleX, scaleY, scaleZ); this.applyMatrix(scaler); }; THREE.ColorCube.prototype = Object.create(THREE.Mesh.prototype); THREE.ColorCube.prototype.constructor = THREE.ColorCube; THREE.ColorCube.prototype.clearVertexNormals = function () { if (this.vNormals === undefined) { this.vNormals = []; } for (var i = 0, len = this.vNormals.length; i < len; ++i) { this.parent.remove(this.vNormals[i]); } this.vNormals.length = 0; } THREE.ColorCube.prototype.drawVertexNormals = function (scale, color) { this.clearVertexNormals(); scale = (scale === undefined) ? 1 : scale; color = (color === undefined) ? 0xff : color; var origin = new THREE.Vector3(), normalArrow = null, vert = null, norm = null, index = null, vertexArray = this.geometry.attributes.position.array, normalArray = this.geometry.attributes.normal.array, indexArray = this.geometry.index.array; for (var i = 0, len = indexArray.length; i < len; ++i) { index = indexArray[i]; vert = new THREE.Vector3(...vertexArray.slice((index * 3), (index * 3) + 3)).applyMatrix4(this.matrix); norm = new THREE.Vector3(...normalArray.slice((index * 3), (index * 3) + 3)); normalArrow = new THREE.ArrowHelper( norm, origin, 1 * scale, color, 0.2 * scale, 0.1 * scale ); normalArrow.position.copy(vert); this.vNormals.push(normalArrow); this.parent.add(normalArrow); } }; THREE.ColorCube.prototype.clearFaceNormals = function () { if (this.fNormals === undefined) { this.fNormals = []; } for (var i = 0, len = this.fNormals.length; i < len; ++i) { this.parent.remove(this.fNormals[i]); } this.fNormals.length = 0; } THREE.ColorCube.prototype.drawFaceNormals = function (scale, color) { this.clearFaceNormals(); scale = (scale === undefined) ? 1 : scale; color = (color === undefined) ? 0xffaa00 : color; var origin = new THREE.Vector3(), normalArrow = null, vertices = [new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3()], normals = [new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3()], indices = [0, 0, 0], centroid = new THREE.Vector3(), faceNormal = new THREE.Vector3(), vertexArray = this.geometry.attributes.position.array, normalArray = this.geometry.attributes.normal.array, indexArray = this.geometry.index.array; for (var i = 0, len = indexArray.length; i < len; i += 3) { indices = indexArray.slice(i, i + 3); vertices[0].set(...vertexArray.slice((indices[0] * 3), (indices[0] * 3) + 3)).applyMatrix4(this.matrix); vertices[1].set(...vertexArray.slice((indices[1] * 3), (indices[1] * 3) + 3)).applyMatrix4(this.matrix); vertices[2].set(...vertexArray.slice((indices[2] * 3), (indices[2] * 3) + 3)).applyMatrix4(this.matrix); normals[0].set(...normalArray.slice((indices[0] * 3), (indices[0] * 3) + 3)); normals[1].set(...normalArray.slice((indices[1] * 3), (indices[1] * 3) + 3)); normals[2].set(...normalArray.slice((indices[2] * 3), (indices[2] * 3) + 3)); centroid.set( (vertices[0].x + vertices[1].x + vertices[2].x) / 3, (vertices[0].y + vertices[1].y + vertices[2].y) / 3, (vertices[0].z + vertices[1].z + vertices[2].z) / 3 ); faceNormal.set( (normals[0].x + normals[1].x + normals[2].x) / 3, (normals[0].y + normals[1].y + normals[2].y) / 3, (normals[0].z + normals[1].z + normals[2].z) / 3 ); faceNormal.normalize(); normalArrow = new THREE.ArrowHelper( faceNormal, origin, 1 * scale, color, 0.2 * scale, 0.1 * scale ); normalArrow.position.copy(centroid); this.fNormals.push(normalArrow); this.parent.add(normalArrow); } }; THREE.ColorCube.prototype.clearAllNormals = function () { THREE.ColorCube.prototype.clearVertexNormals(); THREE.ColorCube.prototype.clearFaceNormals(); } THREE.ColorCube.prototype.drawWireframe = function (color) { if (this.wireframe === undefined) { color = (color === undefined) ? 0 : color; this.wireframe = new THREE.WireframeHelper(this, color); this.parent.add(this.wireframe); } } THREE.ColorCube.prototype.clearWireframe = function () { if (this.wireframe) { this.parent.remove(this.wireframe); delete this.wireframe; } } })(); var cc = new THREE.ColorCube(25, 25, 25); scene.add(cc); cc.drawVertexNormals(25); cc.drawFaceNormals(25); cc.drawWireframe(0xffffff); draw();
As part of prototyping some debugger objects and methods, I created a simple cube with a different color on each side. Rather than creating multiple meshes, I decided to use draw groups and THREE.Multimaterial. Unfortunately, it's only half-working, and I don't understand why.
What IS working is the first two sides (4 triangles). What's NOT working is any of the other sides. None of the other sides are visible. I've added debug code to draw the (index-based) vertex normals, the (index-based, manually calculated) face normals, and the wireframe (THREE.WireframeHelper).
If I increase the count for the second group to 9, nothing happens. If I change the groups to reference different start/count values within the first 8 vertices, those changes work as expected. Doing anything above 8th vertex has no effect.
I've also checked that the drawRange is set to draw from 0 to Infinity. I've also ruled out typos in my data, because otherwise the normals and wireframe wouldn't work. Is there anything else I'm missing? Thanks!
(I found the issue in r76. Code below referenced r79 at the time of writing.)
jsfiddle: http://jsfiddle.net/TheJim01/gumftkm4/
HTML:
<script src="http://threejs.org/build/three.js"></script> <script src="http://threejs.org/examples/js/controls/TrackballControls.js"></script> <script src="http://threejs.org/examples/js/libs/stats.min.js"></script> <div id="host"></div> <script> // INITIALIZE var WIDTH = window.innerWidth, HEIGHT = window.innerHeight, FOV = 35, NEAR = 1, FAR = 1000; var renderer = new THREE.WebGLRenderer({ antialias: true }); renderer.setSize(WIDTH, HEIGHT); document.getElementById('host').appendChild(renderer.domElement); var stats= new Stats(); stats.domElement.style.position = 'absolute'; stats.domElement.style.top = '0'; document.body.appendChild(stats.domElement); var camera = new THREE.PerspectiveCamera(FOV, WIDTH / HEIGHT, NEAR, FAR); camera.position.z = 250; var trackballControl = new THREE.TrackballControls(camera, renderer.domElement); trackballControl.rotateSpeed = 5.0; // need to speed it up a little var scene = new THREE.Scene(); var light = new THREE.PointLight(0xffffff, 1, Infinity); light.position.copy(camera.position); scene.add(light); function draw(){ light.position.copy(camera.position); renderer.render(scene, camera); stats.update(); } trackballControl.addEventListener('change', draw); function navStartHandler(e) { renderer.domElement.addEventListener('mousemove', navMoveHandler); renderer.domElement.addEventListener('mouseup', navEndHandler); } function navMoveHandler(e) { trackballControl.update(); } function navEndHandler(e) { renderer.domElement.removeEventListener('mousemove', navMoveHandler); renderer.domElement.removeEventListener('mouseup', navEndHandler); } renderer.domElement.addEventListener('mousedown', navStartHandler); renderer.domElement.addEventListener('mousewheel', navMoveHandler); </script>
CSS:
html *{ padding: 0; margin: 0; width: 100%; overflow: hidden; } #host { width: 100%; height: 100%; }
JavaScript:
// New Color Cube (function () { var pos = new Float32Array([ // front -1, 1, 1, -1, -1, 1, 1, 1, 1, 1, -1, 1, // right 1, 1, 1, 1, -1, 1, 1, 1, -1, 1, -1, -1, // back 1, 1, -1, 1, -1, -1, -1, 1, -1, -1, -1, -1, // left -1, 1, -1, -1, -1, -1, -1, 1, 1, -1, -1, 1, // top -1, 1, -1, -1, 1, 1, 1, 1, -1, 1, 1, 1, // bottom -1, -1, 1, -1, -1, -1, 1, -1, 1, 1, -1, -1 ]), nor = new Float32Array([ // front 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, // right 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, // back 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, // left -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, // top 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, // bottom 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0 ]), idx = new Uint32Array([ // front 0, 1, 2, 3, 2, 1, // right 4, 5, 6, 7, 6, 5, // back 8, 9, 10, 11, 10, 9, // left 12, 13, 14, 15, 14, 13, // top 16, 17, 18, 19, 18, 17, // bottom 20, 21, 22, 23, 22, 21 ]); var sideColors = new THREE.MultiMaterial([ new THREE.MeshLambertMaterial({ color: 'red' }), // front new THREE.MeshLambertMaterial({ color: 'green' }), // right new THREE.MeshLambertMaterial({ color: 'orange' }), // back new THREE.MeshLambertMaterial({ color: 'blue' }), // left new THREE.MeshLambertMaterial({ color: 'white' }), // top new THREE.MeshLambertMaterial({ color: 'yellow' }) // bottom ]); var cubeGeometry = new THREE.BufferGeometry(); cubeGeometry.addAttribute("position", new THREE.BufferAttribute(pos, 3)); cubeGeometry.addAttribute("normal", new THREE.BufferAttribute(nor, 3)); cubeGeometry.setIndex(new THREE.BufferAttribute(idx, 3)); cubeGeometry.clearGroups(); cubeGeometry.addGroup(0, 6, 0); cubeGeometry.addGroup(6, 6, 1); cubeGeometry.addGroup(12, 6, 2); cubeGeometry.addGroup(18, 6, 3); cubeGeometry.addGroup(24, 6, 4); cubeGeometry.addGroup(30, 6, 5); THREE.ColorCube = function (scaleX, scaleY, scaleZ) { THREE.Mesh.call(this, cubeGeometry, sideColors); var scaler = new THREE.Matrix4().makeScale(scaleX, scaleY, scaleZ); this.applyMatrix(scaler); }; THREE.ColorCube.prototype = Object.create(THREE.Mesh.prototype); THREE.ColorCube.prototype.constructor = THREE.ColorCube; THREE.ColorCube.prototype.clearVertexNormals = function () { if (this.vNormals === undefined) { this.vNormals = []; } for (var i = 0, len = this.vNormals.length; i < len; ++i) { this.parent.remove(this.vNormals[i]); } this.vNormals.length = 0; } THREE.ColorCube.prototype.drawVertexNormals = function (scale, color) { this.clearVertexNormals(); scale = (scale === undefined) ? 1 : scale; color = (color === undefined) ? 0xff : color; var origin = new THREE.Vector3(), normalArrow = null, vert = null, norm = null, index = null, vertexArray = this.geometry.attributes.position.array, normalArray = this.geometry.attributes.normal.array, indexArray = this.geometry.index.array; for (var i = 0, len = indexArray.length; i < len; ++i) { index = indexArray[i]; vert = new THREE.Vector3(...vertexArray.slice((index * 3), (index * 3) + 3)).applyMatrix4(this.matrix); norm = new THREE.Vector3(...normalArray.slice((index * 3), (index * 3) + 3)); normalArrow = new THREE.ArrowHelper( norm, origin, 1 * scale, color, 0.2 * scale, 0.1 * scale ); normalArrow.position.copy(vert); this.vNormals.push(normalArrow); this.parent.add(normalArrow); } }; THREE.ColorCube.prototype.clearFaceNormals = function () { if (this.fNormals === undefined) { this.fNormals = []; } for (var i = 0, len = this.fNormals.length; i < len; ++i) { this.parent.remove(this.fNormals[i]); } this.fNormals.length = 0; } THREE.ColorCube.prototype.drawFaceNormals = function (scale, color) { this.clearFaceNormals(); scale = (scale === undefined) ? 1 : scale; color = (color === undefined) ? 0xffaa00 : color; var origin = new THREE.Vector3(), normalArrow = null, vertices = [new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3()], normals = [new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3()], indices = [0, 0, 0], centroid = new THREE.Vector3(), faceNormal = new THREE.Vector3(), vertexArray = this.geometry.attributes.position.array, normalArray = this.geometry.attributes.normal.array, indexArray = this.geometry.index.array; for (var i = 0, len = indexArray.length; i < len; i += 3) { indices = indexArray.slice(i, i + 3); vertices[0].set(...vertexArray.slice((indices[0] * 3), (indices[0] * 3) + 3)).applyMatrix4(this.matrix); vertices[1].set(...vertexArray.slice((indices[1] * 3), (indices[1] * 3) + 3)).applyMatrix4(this.matrix); vertices[2].set(...vertexArray.slice((indices[2] * 3), (indices[2] * 3) + 3)).applyMatrix4(this.matrix); normals[0].set(...normalArray.slice((indices[0] * 3), (indices[0] * 3) + 3)); normals[1].set(...normalArray.slice((indices[1] * 3), (indices[1] * 3) + 3)); normals[2].set(...normalArray.slice((indices[2] * 3), (indices[2] * 3) + 3)); centroid.set( (vertices[0].x + vertices[1].x + vertices[2].x) / 3, (vertices[0].y + vertices[1].y + vertices[2].y) / 3, (vertices[0].z + vertices[1].z + vertices[2].z) / 3 ); faceNormal.set( (normals[0].x + normals[1].x + normals[2].x) / 3, (normals[0].y + normals[1].y + normals[2].y) / 3, (normals[0].z + normals[1].z + normals[2].z) / 3 ); faceNormal.normalize(); normalArrow = new THREE.ArrowHelper( faceNormal, origin, 1 * scale, color, 0.2 * scale, 0.1 * scale ); normalArrow.position.copy(centroid); this.fNormals.push(normalArrow); this.parent.add(normalArrow); } }; THREE.ColorCube.prototype.clearAllNormals = function () { THREE.ColorCube.prototype.clearVertexNormals(); THREE.ColorCube.prototype.clearFaceNormals(); } THREE.ColorCube.prototype.drawWireframe = function (color) { if (this.wireframe === undefined) { color = (color === undefined) ? 0 : color; this.wireframe = new THREE.WireframeHelper(this, color); this.parent.add(this.wireframe); } } THREE.ColorCube.prototype.clearWireframe = function () { if (this.wireframe) { this.parent.remove(this.wireframe); delete this.wireframe; } } })(); var cc = new THREE.ColorCube(25, 25, 25); scene.add(cc); cc.drawVertexNormals(25); cc.drawFaceNormals(25); cc.drawWireframe(0xffffff); draw();
原文:https://stackoverflow.com/questions/38775928
最满意答案
答案给了你更多的问题
所有它的“扩展”hstore实际上可以存储JSON,而不仅仅是键值所以这个结构也行:
{"key":{"inner_object_key":{"Another_key":"Done!","list":["no","problem"]}}}
所以,首先你的ModelView应该使用自定义转换器
class ExtendedModelView(ModelView): model_form_converter=CustomAdminConverter
转换器本身应该知道如何使用
hstore
方言:class CustomAdminConverter(AdminModelConverter): @converts('sqlalchemy.dialects.postgresql.hstore.HSTORE') def conv_HSTORE(self, field_args, **extra): return DictToHstoreField(**field_args)
您可以看到这一个使用自定义的WTForms字段,可以在两个方向上转换数据:
class DictToHstoreField(TextAreaField): def process_data(self, value): if value is None: value = {} else: for key,obj in value.iteritems(): if (obj.startswith("{") and obj.endswith("}")) or (obj.startswith("[") and obj.endswith("]")): try: value[key]=json.loads(obj) except: pass # self.data=json.dumps(value) def process_formdata(self, valuelist): if valuelist: self.data = json.loads(valuelist[0]) for key,obj in self.data.iteritems(): if isinstance(obj,dict) or isinstance(obj,list): self.data[key]=json.dumps(obj) if isinstance(obj,int): self.data[key]=str(obj)
最后一步将是在应用程序中实际使用这些数据
对于SQLalchemy而言,我并没有把它做成一个很好的方式,因为它被用在了安静的环境中,所以我只在一个方向上采用这种方式,但我认为很容易从这里获得想法,然后完成剩下的工作。
如果你的情况是简单的键值存储,所以不应该做任何额外的事情,只要按原样使用即可。 但是如果你想在代码中的某个地方打开JSON,只要使用它就很简单,只需要在函数中包装即可
if (value.startswith("{") and value.endswith("}")) or (value.startswith("[") and value.endswith("]")): value=json.loads(value)
通过扩展
FormField
并添加一些用于添加/删除字段的javascript,为实际的非JSON方法创建动态字段也是可能的,但这是完全不同的故事,在我的情况下,我需要实际的json存储,带有二十一点和列表: )The answer gives you a bit more then asked
Fist of all it "extends" hstore to be able to store actually JSON, not just key-value So this structure is also OK:
{"key":{"inner_object_key":{"Another_key":"Done!","list":["no","problem"]}}}
So, first of all your ModelView should use custom converter
class ExtendedModelView(ModelView): model_form_converter=CustomAdminConverter
Converter itself should know how to use
hstore
dialect:class CustomAdminConverter(AdminModelConverter): @converts('sqlalchemy.dialects.postgresql.hstore.HSTORE') def conv_HSTORE(self, field_args, **extra): return DictToHstoreField(**field_args)
This one as you can see uses custom WTForms field which converts data in both directions:
class DictToHstoreField(TextAreaField): def process_data(self, value): if value is None: value = {} else: for key,obj in value.iteritems(): if (obj.startswith("{") and obj.endswith("}")) or (obj.startswith("[") and obj.endswith("]")): try: value[key]=json.loads(obj) except: pass # self.data=json.dumps(value) def process_formdata(self, valuelist): if valuelist: self.data = json.loads(valuelist[0]) for key,obj in self.data.iteritems(): if isinstance(obj,dict) or isinstance(obj,list): self.data[key]=json.dumps(obj) if isinstance(obj,int): self.data[key]=str(obj)
The final step will be to actual use this data in application
I did not make it in common nice way for SQLalchemy, since was used with flask-restful, so I have only adoption for flask-restful in one direction, but I think it's easy to get the idea from here and do the rest.
And if your case is simple key-value storage so nothing additionaly should be done, just use it as is. But if you want to unwrap JSON somewhere in code, it's simple like this whenever you use it, just wrap in function
if (value.startswith("{") and value.endswith("}")) or (value.startswith("[") and value.endswith("]")): value=json.loads(value)
Creating dynamical field for actual nice non-JSON way for editing of data also possible by extending
FormField
and adding some javascript for adding/removing fields, but this is whole different story, in my case I needed actual json storage, with blackjack and lists :)
相关问答
更多-
如何在使用flask-admin时自动翻译字段标签?(How to translate field label automatically when using flask-admin?)[2023-06-15]
我的猜测是,在你的情况下, gettext("logistic_amount")实际上并不返回翻译的字符串。 作为一个相当原始的“快速修复”,您可以尝试为您的模型使用自定义admin/model/list.html ,并: {{ _(name) }}替换所有{{ name }} {{ _(name) }} ,然后Jinja应该小心那个。 (为我工作。) 我没有调查过,但我相信有些Babel / BabelEx配置问题。 问候 We need to use lazy_gettext rather than g ... -
实际上很简单: 只需覆盖edit_form中的edit_form并在那里创建字段,并在choices传递查询,如文档中所示 : def edit_form(self): form = model_form(BulkMessage) form.source_address = SelectField( 'Source Address', choices=[(sa.id, sa.title) for sa in SourceAddress.select().wh ...
-
Flask-admin字段未显示(Flask-admin field not shown)[2022-01-20]
如果你将禁用js页面将呈现正确,所以在JS插件select2看起来像问题。 我在flask-admin创建了这个问题: https : //github.com/mrjoes/flask-admin/issues/244 。 If you will disable js page will render right, so look like problem in JS plugin select2. I created issue for this in flask-admin: https://gith ... -
使用flask-admin,我如何获得支持不存在标签的标签字段?(Using flask-admin, how can I get a tag field supporting un-existed tags?)[2022-03-16]
不幸的是,Flask-Admin不公开任何AJAX端点,因此无法在Select2字段中创建新模型。 但是,有办法让它工作: 从管理表单中排除标记字段 将新的Select2TagsField贡献给具有不同名称的表单(因此没有名称冲突) 在显示新/编辑视图之前,将标记列表序列化为字符串数组并添加到Select2TagsField 在提交模型(on_model_change)之前查看列表,执行数据库查找以查找现有标记并为缺失标记创建模型 将标签模型列表贡献给模型 Unfortunately, Flask-Admi ... -
答案给了你更多的问题 所有它的“扩展”hstore实际上可以存储JSON,而不仅仅是键值所以这个结构也行: {"key":{"inner_object_key":{"Another_key":"Done!","list":["no","problem"]}}} 所以,首先你的ModelView应该使用自定义转换器 class ExtendedModelView(ModelView): model_form_converter=CustomAdminConverter 转换器本身应该知道如何使用hs ...
-
如果您有非整数主键,则必须使用force_insert=True调用save()以添加新行。 http://docs.peewee-orm.com/en/latest/peewee/models.html#non-integer-primary-keys-composite-keys-and-other-tricks When you've got a non-integer primary key, you must call save() with force_insert=True to add a ...
-
您可以覆盖on_model_change的on_model_change和on_model_delete方法,这些方法在commit更改之前由flask-admin调用: class TableConfig(ModelView): def on_model_change(self, form, model, is_created): if is_created: sqlalchemy_create_table(model) def on_model_ ...
-
事实证明,如果与该关系相关联的外键具有nullable = False,则flask-admin将该字段标记为flask-wtf中所需的字段。 一旦我从Type类的外键的属性中删除了nullable = False , flask-admin就停止在Type model视图和Area model视图中显示所需的字段。 但是,当我删除nullable = False时,ondelete =“CASCADE”和关系上的级联行为没有更多的理由了。 默认级联更适合。 最终代码如下: class Type(db.Mo ...
-
如果要从flask.ext.admin.contrib.sqla扩展flask.ext.admin.contrib.sqla类,则可以覆盖使用的模板的值。 您可以覆盖模型的列表视图,创建视图或编辑视图,如下所示: class CustomAdminView(ModelView): list_template = "list_template.html" edit_template = "edit_template.html" create_template = "create_tem ...
-
我忘了在我的用户模型中为评论声明backref属性。 I did forget to declare the backref attribute in my User model for the Reviews.