@@ -197,14 +197,31 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
197197
198198 let mut fields = SmallMap :: new ( ) ;
199199
200- let auto_field_export = KeyExport ( AUTO_FIELD ) ;
201- let auto_field_type =
202- self . get_from_export ( ModuleName :: django_models_fields ( ) , None , & auto_field_export) ;
203-
204- // TODO: Extend the solution to handle custom pk
205- if let Some ( id_type) = self . get_django_field_type ( & auto_field_type, cls) {
206- fields. insert ( ID , ClassSynthesizedField :: new ( id_type. clone ( ) ) ) ;
207- fields. insert ( PK , ClassSynthesizedField :: new ( id_type) ) ;
200+ let custom_pk_field = metadata
201+ . django_model_metadata ( )
202+ . and_then ( |dm| dm. custom_primary_key_field . as_ref ( ) ) ;
203+
204+ if let Some ( pk_field_name) = custom_pk_field {
205+ let instance_type = self . as_class_type_unchecked ( cls) . to_type ( ) ;
206+ let pk_attr_type = self . attr_infer_for_type (
207+ & instance_type,
208+ pk_field_name,
209+ TextRange :: default ( ) ,
210+ & self . error_swallower ( ) ,
211+ None ,
212+ ) ;
213+ fields. insert ( PK , ClassSynthesizedField :: new ( pk_attr_type) ) ;
214+ // When there's a custom pk, don't synthesize an `id` field
215+ } else {
216+ // No custom pk, use default AutoField for both id and pk
217+ let auto_field_export = KeyExport ( AUTO_FIELD ) ;
218+ let auto_field_type =
219+ self . get_from_export ( ModuleName :: django_models_fields ( ) , None , & auto_field_export) ;
220+
221+ if let Some ( id_type) = self . get_django_field_type ( & auto_field_type, cls) {
222+ fields. insert ( ID , ClassSynthesizedField :: new ( id_type. clone ( ) ) ) ;
223+ fields. insert ( PK , ClassSynthesizedField :: new ( id_type) ) ;
224+ }
208225 }
209226
210227 Some ( ClassSynthesizedFields :: new ( fields) )
0 commit comments