Create dynamic model in Django 1.8 - python

How to create Dynamic models in Django 1.8?
I have done the following according to the official site:
>>attrs = {
'name': models.CharField(max_length=32),
'__module__': 'myapp.models'
}
>>person = type("ptable", (models.Model,), attrs)
>>def install(model):
from django.core.management import sql, color
from django.db import connection
style = color.no_style()
cursor = connection.cursor()
statements, pending = sql.sql_model_create(model, style)
for sql in statements:
cursor.execute(sql)
>>install(person)
But it gives me this error:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "<console>", line 6, in install
AttributeError: 'module' object has no attribute 'sql_model_create'
How to solve it?

Related

Unable to create owl:NamedIndividual on owlready2

I need to create a NamedIndividual (https://www.w3.org/TR/owl2-syntax/#Named_Individuals) but I am not seeing a way to do it.
The following example:
from owlready2 import *
onto = get_ontology("example")
class NamedIndividual(Thing): namespace = owl
print(NamedIndividual.iri)
print(NamedIndividual.name)
mini = NamedIndividual()
onto.save("minrep.rdf")
Gives the following error:
http://www.w3.org/2002/07/owl#NamedIndividual
NamedIndividual
Traceback (most recent call last):
File "minrep.py", line 13, in <module>
mini = NamedIndividual("example")
File "/home/lubianat/Documents/main_venv/lib/python3.8/site-packages/owlready2/individual.py", line 137, in __init__
if self.storid > 0: self.namespace.ontology._add_obj_triple_spo(self.storid, rdf_type, owl_named_individual)
TypeError: '>' not supported between instances of 'str' and 'int'
If I instantiate the class without a name, I get another error:
http://www.w3.org/2002/07/owl#NamedIndividual
NamedIndividual
Traceback (most recent call last):
File "minrep.py", line 13, in <module>
mini = NamedIndividual()
File "/home/lubianat/Documents/main_venv/lib/python3.8/site-packages/owlready2/individual.py", line 123, in __init__
iri = self.namespace.world._new_numbered_iri("%s%s" % (self.namespace._base_iri, self.generate_default_name()))
AttributeError: 'World' object has no attribute '_new_numbered_iri'
Any ideas on what is happening there? Thanks!

How do i make TinyDB file

import simplejson as json
from tinydb import TinyDB
#파일 DB 생성
db = TinyDB('C:\python\\section5\\databases\\database.db')
#db.default_table_name = 'users'
This is ERROR
please help me to solve this problem:
Traceback (most recent call last):
File "C:\python\section5\5-2-1.py", line 3, in <module>
from tinydb.storages import MemoryStorage
File "C:\Users\HSM\anaconda3\envs\section5\lib\site-packages\tinydb\__init__.py", line 29, in <module>
from .database import TinyDB
File "C:\Users\HSM\anaconda3\envs\section5\lib\site-packages\tinydb\database.py", line 13
TableBase: Type[Table] = with_typehint(Table)
^
SyntaxError: invalid syntax
[Finished in 0.078s]
this means you're using old version python,you can do pip install tinydb==3.5.0(lastest version 4.5.0 make error)

Error while working with sqlite3 in python

In my Python script, I am seeing the following error.
Traceback (most recent call last):
File "F:/python sqllite/test32.py", line 3,
in <module> conn.sqlite3.connect('test.db')
AttributeError: 'sqlite3.Connection' object has no attribute 'sqlite3'
Does anyone have an idea why I might be seeing this?
Change: conn.sqlite3.connect('test.db')
to : conn = sqlite3.connect('test.db')

Python multiprocessing: Share data from dynamically created module on windows

I'd like to send data defined in a module that was dynamically loaded with imp via a Manager().Queue() on Windows. Is this possible? Here is a minimal testcase to demonstrate what I mean:
import imp
import sys
from multiprocessing import Manager
if __name__ == '__main__':
s = """
def payload():
print("It works!")
"""
mod = imp.new_module('testcase')
exec s in mod.__dict__
sys.modules['testcase'] = mod
payload = mod.payload
payload() # It works!
m = Manager()
queue = m.Queue()
queue.put(payload) # AttributeError: 'module' object has no attribute 'payload'
Note: The use of imp.new_module + exec is just to get the testcase in a single file. The same AttributeError is raised when using imp.load_source.
Note2: This testcase leaves out all the Pool/worker code as the error happens before then.
Here is the output with full traceback from running the above script:
It works!
Traceback (most recent call last):
File "testcase.py", line 23, in <module>
queue.put(payload) # AttributeError: 'module' object has no attribute 'payload'
File "<string>", line 2, in put
File "c:\Users\Andrew\dev\python\lib\multiprocessing\managers.py", line 774, in _callmethod
raise convert_to_error(kind, result)
multiprocessing.managers.RemoteError:
---------------------------------------------------------------------------
Traceback (most recent call last):
File "c:\Users\Andrew\dev\python\lib\multiprocessing\managers.py", line 240, in serve_client
request = recv()
AttributeError: 'module' object has no attribute 'payload'
---------------------------------------------------------------------------
Thanks!

SugarCRM Python API Wrapper - "NoneType" object is not subscriptable

I downloaded the SugarCRM Python API wrapper from Github
Trying out the basic login code, I have the following problem
>>> url = 'http://<REDACTED>/service/v2/rest.php'
>>> user = '<REDACTED>'
>>> passwd = '<REDACTED>'
>>> import sugarcrm
>>> session = sugarcrm.Sugarcrm(url, user, passwd)
Connecting to: http://<REDACTED>/service/v2/rest.php
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/sugarcrm/sugarcrm.py", line 58, in __init__
self.login(username, password)
File "/usr/local/lib/python2.7/dist-packages/sugarcrm/sugarcrm.py", line 112, in login
self.id = x["id"]
TypeError: 'NoneType' object is not subscriptable
Am I doing something wrong?
There is a workaround here:
https://github.com/sugarcrm/python_webservices_library/issues/3

Categories

Resources