79720141

Date: 2025-07-30 14:56:42
Score: 0.5
Natty:
Report link

1.Why two methods?

to_python() is called in all contexts (DB reads, form validation, direct assignment like obj.field = "xyz").
from_db_value() is only called for DB reads (and only if defined—older Django versions relied solely on to_python() for DB values).

2.Why does to_python() check for Hand instances?

If you do obj.hand = Hand(...) in code, Django calls to_python() on that Hand instance. The check avoids re-parsing an already-correct object. However, from_db_value() never gets a Hand instance. Database values are always raw strings (e.g., "AKQJ..."). So it skips this check.

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Low reputation (1):
Posted by: luxiu lu