Skip to content
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

Merged
merged 3 commits into from Sep 26, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions flask_sqlalchemy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

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...

Copy link
Member

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

repr() instead of str()? slightly uglier in py2 because of the u prefix, but pretty much all PKs are numeric anyway

return '<{0} {1}>'.format(self.__class__.__name__, pk)
Copy link
Contributor

@ThiefMaster ThiefMaster Sep 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK type(self) is the preferred way to get the class

Copy link
Member

@davidism davidism Sep 26, 2017

Choose a reason for hiding this comment

The 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, type just means one less function call (not that it's a huge deal here).



class SQLAlchemy(object):
Expand Down
33 changes: 21 additions & 12 deletions tests/test_model_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Member

@davidism davidism Sep 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, identity_key isn't populated until the object is persisted.

Copy link
Contributor

Choose a reason for hiding this comment

The 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)