-
-
Notifications
You must be signed in to change notification settings - Fork 900
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add default __repr__ for Model class #530
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -663,9 +663,8 @@ class Model(object): | |
query = None | ||
|
||
def __repr__(self): | ||
pks = inspect(self).identity_key[1] | ||
pretty_pk = pks[0] if len(pks) == 1 else ', '.join(map(str, pks)) | ||
return '<{0}({1})>'.format(type(self).__name__, pretty_pk) | ||
pk = ', '.join(str(value) for value in inspect(self).identity) | ||
return '<{0} {1}>'.format(self.__class__.__name__, pk) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFAIK There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, first result for "type(self) vs self.class": https://stackoverflow.com/questions/10386166/python-self-class-vs-typeself 😄 They're equivalent, |
||
|
||
|
||
class SQLAlchemy(object): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,15 +13,24 @@ class SomeModel(db.Model): | |
assert isinstance(SomeModel(), CustomModelClass) | ||
|
||
|
||
def test_default_repr_works_when_no_override(app): | ||
class CustomModelClass(fsa.Model): | ||
pass | ||
|
||
db = fsa.SQLAlchemy(app, model_class=CustomModelClass) | ||
|
||
class SomeModel(db.Model): | ||
id = db.Column(db.Integer, primary_key=True) | ||
|
||
instance = SomeModel() | ||
|
||
assert instance.__repr__, "<SomeModel({})>".format(instance.pk) | ||
def test_repr(db): | ||
class User(db.Model): | ||
name = db.Column(db.String, primary_key=True) | ||
|
||
class Report(db.Model): | ||
id = db.Column(db.Integer, primary_key=True, autoincrement=False) | ||
user_name = db.Column(db.ForeignKey(User.name), primary_key=True) | ||
|
||
db.create_all() | ||
|
||
u = User(name='test') | ||
db.session.add(u) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think create_all, add and flush aren't needed here since you don't need a database roundtrip to access the attributes you just set There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah, good point |
||
db.session.flush() | ||
assert repr(u) == '<User test>' | ||
assert repr(u) == str(u) | ||
|
||
r = Report(id=2, user_name=u.name) | ||
db.session.add(r) | ||
db.session.flush() | ||
assert repr(r) == '<Report 2, test>' | ||
assert repr(u) == str(u) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's test the
str(value)
with a PK that contains non-ascii characters. such a PK is pretty 💩 but you never know...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, that fails. Have a suggestion how to rewrite this for Unicode on Py2? Will need to dig into Flask's compat stuff, maybe there's something there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
repr()
instead ofstr()
? slightly uglier in py2 because of theu
prefix, but pretty much all PKs are numeric anyway