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

Defaults/calculated fields #77

Closed
wants to merge 4 commits into from

Conversation

joshowen
Copy link
Contributor

And only calculate defaults if they aren't set already.

We've found this very helpful, let me know what you guys think. Obviously it requires some documentation.

@joshowen joshowen mentioned this pull request Sep 23, 2015
6 tasks
@joshowen
Copy link
Contributor Author

We've been using this as follows:

class Position(pynamodb.models.Model):
    portfolio_id = NumberAttribute()
    position_date = DateAttribute()
    status = NumberAttribute(null=True)
    portfolio_date = BinaryAttribute(hash_key=True, default=lambda x: to_dated_attribute(x.position_date, x.portfolio_id))
    most_current = BooleanAttribute(null=True, calculator=lambda x: x.status == PositionStatus.MOST_CURRENT.value or None)

By encapsulating this logic in the model, we've been able to simplify a lot of our Position creation code.

  • most_current is calculated on every object save
  • portfolio_date is calculated at object creation

@joshowen
Copy link
Contributor Author

@jlafon any thoughts

@jlafon
Copy link
Contributor

jlafon commented Nov 23, 2015

@joshowen That really makes for succinct code! Can we add some tests?

@pappacena
Copy link

+1

@joshowen
Copy link
Contributor Author

I'll try to add some tests this week or next.

@garrettheel garrettheel closed this May 2, 2017
@garrettheel garrettheel changed the base branch from devel to master May 2, 2017 22:05
@garrettheel
Copy link
Member

Reopening with new default branch

@garrettheel garrettheel reopened this May 2, 2017
@joshowen joshowen closed this Jul 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants