111
111
RECURSIVE_REFERENCE_CONSTANT = "self"
112
112
113
113
114
+ def _unsaved_object_error (document ):
115
+ return (
116
+ f"The instance of the document '{ document } ' you are "
117
+ "trying to reference has an empty 'id'. You can only reference "
118
+ "documents once they have been saved to the database"
119
+ )
120
+
121
+
114
122
class StringField (BaseField ):
115
123
"""A unicode string field."""
116
124
@@ -1215,10 +1223,7 @@ def to_mongo(self, document):
1215
1223
1216
1224
# XXX ValidationError raised outside of the "validate" method.
1217
1225
if id_ is None :
1218
- self .error (
1219
- "You can only reference documents once they have"
1220
- " been saved to the database"
1221
- )
1226
+ self .error (_unsaved_object_error (document .__class__ .__name__ ))
1222
1227
1223
1228
# Use the attributes from the document instance, so that they
1224
1229
# override the attributes of this field's document type
@@ -1262,10 +1267,7 @@ def validate(self, value):
1262
1267
)
1263
1268
1264
1269
if isinstance (value , Document ) and value .id is None :
1265
- self .error (
1266
- "You can only reference documents once they have been "
1267
- "saved to the database"
1268
- )
1270
+ self .error (_unsaved_object_error (value .__class__ .__name__ ))
1269
1271
1270
1272
def lookup_member (self , member_name ):
1271
1273
return self .document_type ._fields .get (member_name )
@@ -1370,10 +1372,7 @@ def to_mongo(self, document, use_db_field=True, fields=None):
1370
1372
# We need the id from the saved object to create the DBRef
1371
1373
id_ = document .pk
1372
1374
if id_ is None :
1373
- self .error (
1374
- "You can only reference documents once they have"
1375
- " been saved to the database"
1376
- )
1375
+ self .error (_unsaved_object_error (document .__class__ .__name__ ))
1377
1376
else :
1378
1377
self .error ("Only accept a document object" )
1379
1378
@@ -1394,10 +1393,7 @@ def prepare_query_value(self, op, value):
1394
1393
# XXX ValidationError raised outside of the "validate" method.
1395
1394
if isinstance (value , Document ):
1396
1395
if value .pk is None :
1397
- self .error (
1398
- "You can only reference documents once they have"
1399
- " been saved to the database"
1400
- )
1396
+ self .error (_unsaved_object_error (value .__class__ .__name__ ))
1401
1397
value_dict = {"_id" : value .pk }
1402
1398
for field in self .fields :
1403
1399
value_dict .update ({field : value [field ]})
@@ -1411,10 +1407,7 @@ def validate(self, value):
1411
1407
self .error ("A CachedReferenceField only accepts documents" )
1412
1408
1413
1409
if isinstance (value , Document ) and value .id is None :
1414
- self .error (
1415
- "You can only reference documents once they have been "
1416
- "saved to the database"
1417
- )
1410
+ self .error (_unsaved_object_error (value .__class__ .__name__ ))
1418
1411
1419
1412
def lookup_member (self , member_name ):
1420
1413
return self .document_type ._fields .get (member_name )
@@ -1513,10 +1506,7 @@ def validate(self, value):
1513
1506
1514
1507
# We need the id from the saved object to create the DBRef
1515
1508
elif isinstance (value , Document ) and value .id is None :
1516
- self .error (
1517
- "You can only reference documents once they have been"
1518
- " saved to the database"
1519
- )
1509
+ self .error (_unsaved_object_error (value .__class__ .__name__ ))
1520
1510
1521
1511
def to_mongo (self , document ):
1522
1512
if document is None :
@@ -1533,10 +1523,7 @@ def to_mongo(self, document):
1533
1523
id_ = document .id
1534
1524
if id_ is None :
1535
1525
# XXX ValidationError raised outside of the "validate" method.
1536
- self .error (
1537
- "You can only reference documents once they have"
1538
- " been saved to the database"
1539
- )
1526
+ self .error (_unsaved_object_error (document .__class__ .__name__ ))
1540
1527
else :
1541
1528
id_ = document
1542
1529
@@ -2535,10 +2522,7 @@ def validate(self, value):
2535
2522
)
2536
2523
2537
2524
if pk is None :
2538
- self .error (
2539
- "You can only reference documents once they have been "
2540
- "saved to the database"
2541
- )
2525
+ self .error (_unsaved_object_error (self .document_type .__name__ ))
2542
2526
2543
2527
def prepare_query_value (self , op , value ):
2544
2528
if value is None :
@@ -2607,8 +2591,9 @@ def __get__(self, instance, owner):
2607
2591
def validate (self , value ):
2608
2592
if isinstance (value , LazyReference ) and value .pk is None :
2609
2593
self .error (
2610
- "You can only reference documents once they have been"
2611
- " saved to the database"
2594
+ _unsaved_object_error (
2595
+ self .__class__ .__name__
2596
+ ) # Actual class is difficult to predict here
2612
2597
)
2613
2598
return super ().validate (value )
2614
2599
0 commit comments