Skip to content

Pre-select routine weight units from the user profile (#2205) - #1

Draft
Dmao233 wants to merge 1 commit into
masterfrom
cursor/preselect-weight-units-2879
Draft

Pre-select routine weight units from the user profile (#2205)#1
Dmao233 wants to merge 1 commit into
masterfrom
cursor/preselect-weight-units-2879

Conversation

@Dmao233

@Dmao233 Dmao233 commented Aug 30, 2026

Copy link
Copy Markdown
Owner

Proposed Changes

When creating a SlotEntry or WorkoutLog without an explicit weight_unit, the API always stored kilograms because that is the model default. Users who set weight_unit to lb on their profile still got kg on new routine entries.

This PR defaults the unit from the owner's profile only on create:

  • New slot entries use the routine owner's profile (kg → unit 1, lb → unit 2).
  • New workout logs use the log owner's profile (REST user= and PowerSync user_id=).
  • An explicit weight_unit in the request, including null, is kept.
  • PATCH / edits do not rewrite the stored unit if the profile later changes.

The model default remains kg, so the serializer inspects initial_data instead of validated_data. Checking validated_data after DRF applies the model default cannot tell "client omitted the field" from "client sent kg".

Web/Flutter UI pre-selection still belongs in wger-project/react and the Flutter app. This repo only owns the API default used when a client omits the field.

Related Issue(s)

Fixes wger-project#2205

See also open PRs wger-project#2264 and wger-project#2439, which attempt the same feature in perform_create. Those stay open; this version avoids the model-default trap, covers workout logs (including PowerSync create), and adds the new-vs-edit cases from the issue discussion.

Please check that the PR fulfills these requirements

  • Tests for the changes have been added (for bug fixes / features)
  • Code has been formatted to avoid unnecessary diffs (ruff format && isort .)
  • If the feature is big enough or if there are manual steps needed (deployment changes etc.), write a small writeup in CHANGELOG.md

Testing

DJANGO_SETTINGS_MODULE=settings.ci uv run ./manage.py test \
  wger.manager.tests.test_api_weight_unit_default \
  wger.manager.tests.test_log_session_api \
  wger.manager.tests.test_api_ownership_checks \
  wger.manager.tests.test_weight_log \
  wger.manager.tests.test_copy_routine

8 new tests plus 39 related existing tests, all passing.

Open in Web Open in Cursor 

)

New SlotEntry and WorkoutLog creates omitted weight_unit and always
stored kg because that is the model default. Default the unit from the
owner's profile on create only, and keep any explicit value or PATCH
so historical entries are not rewritten when the profile changes.

Verified with test_api_weight_unit_default (8 tests) and the related
log session, ownership, weight log, and copy-routine tests (47 total).

Co-authored-by: CenFangyu <Dmao233@users.noreply.github.com>
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.

Pre-select weight units

2 participants