Question

I'm using the redis object mapper, and when I try to run this code:

from redis import *
from rom import util
from rom import *

util.set_connection_settings(host='localhost', db=7)
class User(Model):
    uName=String(required=True)
    password=String(required=True)
    thisId=(random.random()*100)


user=User(uName='pfleet',password='pass')
user.save()
#todo, save session ID
user2=User.get_by(uName='pfleet')

For some reason, when I run this code, I get an error that says 'Name String not defined', even though I typed the code in the same way as the online example on the ROM(redis object mapper) github page.

Était-ce utile?

La solution

Thanks to the @univerio's comment, it appears that String is imported by from rom import * only on python2.x, quote from the docstring:

note:: this column type is only available in Python 2.x

For python3.x you need to use Text instead.

class User(Model):
    uName=Text(required=True)
    password=Text(required=True)

As a side note, try not to use from xxx import * wildcard imports - it's really a bad practice:

Wildcard imports (from import *) should be avoided, as they make it unclear which names are present in the namespace, confusing both readers and many automated tools. There is one defensible use case for a wildcard import, which is to republish an internal interface as part of a public API (for example, overwriting a pure Python implementation of an interface with the definitions from an optional accelerator module and exactly which definitions will be overwritten isn't known in advance).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top