(self, value)
| 2508 | return value |
| 2509 | |
| 2510 | def validate(self, value): |
| 2511 | if isinstance(value, LazyReference): |
| 2512 | if value.collection != self.document_type._get_collection_name(): |
| 2513 | self.error("Reference must be on a `%s` document." % self.document_type) |
| 2514 | pk = value.pk |
| 2515 | elif isinstance(value, self.document_type): |
| 2516 | pk = value.pk |
| 2517 | elif isinstance(value, DBRef): |
| 2518 | # TODO: check collection ? |
| 2519 | collection = self.document_type._get_collection_name() |
| 2520 | if value.collection != collection: |
| 2521 | self.error("DBRef on bad collection (must be on `%s`)" % collection) |
| 2522 | pk = value.id |
| 2523 | else: |
| 2524 | # value is the primary key of the referenced document |
| 2525 | id_field_name = self.document_type._meta["id_field"] |
| 2526 | id_field = getattr(self.document_type, id_field_name) |
| 2527 | pk = value |
| 2528 | try: |
| 2529 | id_field.validate(pk) |
| 2530 | except ValidationError: |
| 2531 | self.error( |
| 2532 | "value should be `{0}` document, LazyReference or DBRef on `{0}` " |
| 2533 | "or `{0}`'s primary key (i.e. `{1}`)".format( |
| 2534 | self.document_type.__name__, type(id_field).__name__ |
| 2535 | ) |
| 2536 | ) |
| 2537 | |
| 2538 | if pk is None: |
| 2539 | self.error( |
| 2540 | "You can only reference documents once they have been " |
| 2541 | "saved to the database" |
| 2542 | ) |
| 2543 | |
| 2544 | def prepare_query_value(self, op, value): |
| 2545 | if value is None: |
nothing calls this directly
no test coverage detected