OneToOneField¶
OneToOneField is the unique variant of ForeignKey.
It behaves like a foreign key at declaration and query time, but the generated relation columns are unique and the reverse accessor is singular by default.
Practical example¶
class Profile(saffier.Model):
id = saffier.IntegerField(primary_key=True, autoincrement=True)
user = saffier.OneToOneField("User", on_delete=saffier.CASCADE)
class Meta:
registry = models
Reverse behavior¶
When related_name is omitted, Saffier generates a singular reverse accessor
from the declaring model name. That means a Profile.user relation typically
becomes user.profile, not user.profiles_set.
For reverse one-to-one relations, remove() can omit the child object because
at most one related row can exist.
saffier.OneToOneField
¶
OneToOneField(to, **kwargs)
Bases: ForeignKey
Foreign-key field that enforces a unique reverse relationship.
The field is implemented as a standard foreign key with unique=True, which
gives it one-to-one semantics while reusing the full foreign-key machinery.
Source code in saffier/core/db/fields/base.py
1554 1555 1556 | |
inject_default_on_partial_update
instance-attribute
¶
inject_default_on_partial_update = get(
"inject_default_on_partial_update", False
)
force_cascade_deletion_relation
instance-attribute
¶
force_cascade_deletion_relation = (
force_cascade_deletion_relation
)
target_registry
property
writable
¶
target_registry
Return the registry used to resolve string relation targets.
Explicit target_registry overrides take precedence, followed by the
owning model registry and finally the field-level registry fallback.
target
property
¶
target
Resolve and cache the target model class for this relation.
String targets are looked up lazily so models can reference classes that are declared later in the import graph.
| RETURNS | DESCRIPTION |
|---|---|
Any
|
Resolved target model class.
TYPE:
|
related_columns
property
¶
related_columns
Return target columns used by the relation mapping.
Composite foreign keys can point to multiple target columns. When
explicit related_fields are not provided, the target model primary-key
columns are used.
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Column | None]
|
dict[str, sqlalchemy.Column | None]: Mapping of target column names to |
dict[str, Column | None]
|
SQLAlchemy columns where available. |
ForeignKeyValidator
¶
ForeignKeyValidator(
*,
title="",
description="",
help_text="",
default=NO_DEFAULT,
null=False,
read_only=False,
**kwargs,
)
Bases: SaffierField
Source code in saffier/core/db/fields/_internal.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | |
__hash__
¶
__hash__()
Source code in saffier/core/datastructures.py
19 20 21 22 23 24 25 26 | |
validate_or_error
¶
validate_or_error(value)
Source code in saffier/core/db/fields/_internal.py
71 72 73 74 75 76 | |
has_default
¶
has_default()
Source code in saffier/core/db/fields/_internal.py
78 79 80 81 82 | |
validation_error
¶
validation_error(code, value=None)
Source code in saffier/core/db/fields/_internal.py
84 85 86 | |
get_error_message
¶
get_error_message(code)
Source code in saffier/core/db/fields/_internal.py
88 89 | |
get_default_value
¶
get_default_value()
Source code in saffier/core/db/fields/_internal.py
91 92 93 94 95 | |
check
¶
check(value)
Source code in saffier/core/db/fields/base.py
681 682 683 684 685 686 687 688 | |
get_validator
¶
get_validator(**kwargs)
Source code in saffier/core/db/fields/base.py
781 782 | |
get_column_type
¶
get_column_type()
Source code in saffier/core/db/fields/base.py
130 131 | |
get_constraints
¶
get_constraints()
Source code in saffier/core/db/fields/base.py
133 134 | |
get_columns
¶
get_columns(name)
Source code in saffier/core/db/fields/base.py
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 | |
get_global_constraints
¶
get_global_constraints(name, columns, *, schema=None)
Build cross-table constraints and indexes for the foreign key columns.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Logical field name on the owning model.
TYPE:
|
columns
|
Physical columns generated for the relation.
TYPE:
|
schema
|
Optional schema override for the target table reference.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Sequence[Constraint | Index]
|
typing.Sequence[sqlalchemy.Constraint | sqlalchemy.Index]: SQLAlchemy constraints and indexes that should be attached to the owning table. |
Source code in saffier/core/db/fields/base.py
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 | |
has_column
¶
has_column()
Source code in saffier/core/db/fields/base.py
149 150 | |
get_embedded_fields
¶
get_embedded_fields(field_name, existing_fields)
Source code in saffier/core/db/fields/base.py
152 153 154 155 156 157 158 | |
expand_relationship
¶
expand_relationship(value)
Normalize relation values into lightweight target model instances.
Raw primary-key values, dictionaries, proxy models, and already-loaded target instances are all accepted so callers can assign foreign keys in a natural way while the ORM preserves a consistent in-memory shape.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
Incoming relation value.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Any
|
typing.Any: Target model instance or |
Source code in saffier/core/db/fields/base.py
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 | |
clean
¶
clean(name, value, *, for_query=False)
Source code in saffier/core/db/fields/base.py
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 | |
modify_input
¶
modify_input(name, kwargs)
Source code in saffier/core/db/fields/base.py
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 | |
raise_for_non_default
¶
raise_for_non_default(default, server_default)
Source code in saffier/core/db/fields/base.py
185 186 | |
get_default_value
¶
get_default_value()
Source code in saffier/core/db/fields/base.py
188 189 | |
has_default
¶
has_default()
Source code in saffier/core/db/fields/base.py
191 192 | |
get_default_values
¶
get_default_values(field_name, cleaned_data)
Source code in saffier/core/db/fields/base.py
194 195 196 197 198 199 200 201 | |
is_required
¶
is_required()
Source code in saffier/core/db/fields/base.py
203 204 205 206 | |
get_is_null_clause
¶
get_is_null_clause(column)
Source code in saffier/core/db/fields/base.py
208 209 | |
get_is_empty_clause
¶
get_is_empty_clause(column)
Source code in saffier/core/db/fields/base.py
211 212 | |
operator_to_clause
¶
operator_to_clause(field_name, operator, table, value)
Translate one lookup operator into a SQLAlchemy clause.
| PARAMETER | DESCRIPTION |
|---|---|
field_name
|
Logical field name being filtered.
TYPE:
|
operator
|
Saffier lookup suffix such as
TYPE:
|
table
|
SQLAlchemy table containing the target column.
TYPE:
|
value
|
Lookup value supplied by the caller.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Any
|
SQLAlchemy boolean clause implementing the lookup.
TYPE:
|
Source code in saffier/core/db/fields/base.py
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | |
get_fk_name
¶
get_fk_name(name)
Source code in saffier/core/db/fields/base.py
817 818 819 | |
get_fkindex_name
¶
get_fkindex_name(name)
Source code in saffier/core/db/fields/base.py
821 822 823 | |
get_fk_field_name
¶
get_fk_field_name(name, fieldname)
Source code in saffier/core/db/fields/base.py
825 826 827 828 | |
get_fk_column_name
¶
get_fk_column_name(name, fieldname)
Source code in saffier/core/db/fields/base.py
830 831 832 833 834 | |
get_column_names
¶
get_column_names(name)
Source code in saffier/core/db/fields/base.py
836 837 838 839 | |
from_fk_field_name
¶
from_fk_field_name(name, fieldname)
Source code in saffier/core/db/fields/base.py
841 842 843 844 | |
pre_save_callback
async
¶
pre_save_callback(value, original_value, is_update)
Save nested related objects before persisting the owning row.
The callback allows callers to pass an unsaved related model instance or a dictionary payload. When necessary, the related object is saved first and the method returns the concrete foreign-key column values to persist on the owning model.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
Current relation value being persisted.
TYPE:
|
original_value
|
Previous relation value used during updates.
TYPE:
|
is_update
|
Whether the owning model is performing an update.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any]
|
dict[str, typing.Any]: Database-ready foreign-key column payload. |
Source code in saffier/core/db/fields/base.py
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 | |
is_cross_db
¶
is_cross_db(owner_database=None)
Source code in saffier/core/db/fields/base.py
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 | |
get_related_model_for_admin
¶
get_related_model_for_admin()
Source code in saffier/core/db/fields/base.py
1148 1149 1150 1151 1152 1153 1154 | |
get_column
¶
get_column(name)
Source code in saffier/core/db/fields/base.py
1558 1559 | |