Skip to main content

pyforge_django/
model.rs

1// Author: Abdulwahed Mansour
2//! Django model field extraction and type mapping.
3//!
4//! This module provides the bridge between Django's Python model `_meta` API
5//! and the Rust-native `FieldDescriptor` system. It extracts field definitions
6//! from a live Django model class and converts Python field values into
7//! `FieldValue` variants for processing by the serializer and validator.
8
9use chrono::{DateTime, NaiveDate, NaiveTime, Utc};
10use pyforge::prelude::*;
11use rust_decimal::Decimal;
12use std::str::FromStr;
13use uuid::Uuid;
14
15use crate::error::DjangoError;
16use crate::field_types::{DjangoFieldType, FieldDescriptor};
17use pyforge_core::types::FieldValue;
18
19/// Extracts field descriptors from a Django model class via its `_meta` API.
20///
21/// Introspects the model's `_meta.get_fields()` result and maps each concrete
22/// field to a `FieldDescriptor`. Relation fields (ForeignKey, ManyToMany) are
23/// skipped — they require separate handling via Django's ORM query layer.
24///
25/// # Arguments
26/// * `model_class` - A Python reference to a Django model class (not an instance).
27///
28/// # Returns
29/// A `Vec<FieldDescriptor>` for all concrete (non-relational) fields on the model,
30/// or `DjangoError` if the class lacks a `_meta` attribute or introspection fails.
31pub fn extract_field_descriptors(
32    py: Python<'_>,
33    model_class: &Bound<'_, PyAny>,
34) -> Result<Vec<FieldDescriptor>, DjangoError> {
35    let meta = model_class
36        .getattr("_meta")
37        .map_err(|_| DjangoError::Python("model class has no _meta attribute".into()))?;
38
39    let fields = meta
40        .call_method0("get_fields")
41        .map_err(|e| DjangoError::Python(format!("_meta.get_fields() failed: {e}")))?;
42
43    let mut descriptors = Vec::new();
44
45    let fields_list: Vec<Bound<'_, PyAny>> = fields
46        .extract()
47        .map_err(|e| DjangoError::Python(format!("cannot iterate _meta.get_fields(): {e}")))?;
48
49    for field in &fields_list {
50
51        // Skip relation fields — they don't have a direct column representation
52        let is_relation = field
53            .getattr("is_relation")
54            .and_then(|v| v.extract::<bool>())
55            .unwrap_or(true);
56        if is_relation {
57            continue;
58        }
59
60        let name: String = field
61            .getattr("name")
62            .and_then(|v| v.extract())
63            .map_err(|e| DjangoError::Python(format!("field missing 'name': {e}")))?;
64
65        let internal_type: String = field
66            .call_method0("get_internal_type")
67            .and_then(|v| v.extract())
68            .unwrap_or_default();
69
70        let nullable: bool = field
71            .getattr("null")
72            .and_then(|v| v.extract())
73            .unwrap_or(false);
74
75        let has_default: bool = field
76            .call_method0("has_default")
77            .and_then(|v| v.extract())
78            .unwrap_or(false);
79
80        if let Some(field_type) = map_django_internal_type(py, field, &internal_type) {
81            descriptors.push(FieldDescriptor {
82                name,
83                field_type,
84                nullable,
85                has_default,
86            });
87        }
88    }
89
90    Ok(descriptors)
91}
92
93/// Maps a Django field's `get_internal_type()` string to a `DjangoFieldType`.
94///
95/// Reads constraint attributes (max_length, max_digits, decimal_places) directly
96/// from the Python field object.
97fn map_django_internal_type(
98    _py: Python<'_>,
99    field: &Bound<'_, PyAny>,
100    internal_type: &str,
101) -> Option<DjangoFieldType> {
102    match internal_type {
103        "CharField" => {
104            let max_length = extract_usize_attr(field, "max_length").unwrap_or(255);
105            Some(DjangoFieldType::CharField { max_length })
106        }
107        "TextField" => Some(DjangoFieldType::TextField),
108        "IntegerField" | "SmallIntegerField" | "PositiveIntegerField"
109        | "PositiveSmallIntegerField" => Some(DjangoFieldType::IntegerField),
110        "BigIntegerField" | "PositiveBigIntegerField" => Some(DjangoFieldType::BigIntegerField),
111        "FloatField" => Some(DjangoFieldType::FloatField),
112        "DecimalField" => {
113            let max_digits = extract_u32_attr(field, "max_digits").unwrap_or(10);
114            let decimal_places = extract_u32_attr(field, "decimal_places").unwrap_or(2);
115            Some(DjangoFieldType::DecimalField {
116                max_digits,
117                decimal_places,
118            })
119        }
120        "BooleanField" | "NullBooleanField" => Some(DjangoFieldType::BooleanField),
121        "DateField" => Some(DjangoFieldType::DateField),
122        "TimeField" => Some(DjangoFieldType::TimeField),
123        "DateTimeField" => Some(DjangoFieldType::DateTimeField),
124        "UUIDField" => Some(DjangoFieldType::UuidField),
125        "JSONField" => Some(DjangoFieldType::JsonField),
126        "BinaryField" => {
127            let max_length = extract_usize_attr(field, "max_length");
128            Some(DjangoFieldType::BinaryField { max_length })
129        }
130        "EmailField" => {
131            let max_length = extract_usize_attr(field, "max_length").unwrap_or(254);
132            Some(DjangoFieldType::EmailField { max_length })
133        }
134        "URLField" => {
135            let max_length = extract_usize_attr(field, "max_length").unwrap_or(200);
136            Some(DjangoFieldType::UrlField { max_length })
137        }
138        "SlugField" => {
139            let max_length = extract_usize_attr(field, "max_length").unwrap_or(50);
140            Some(DjangoFieldType::SlugField { max_length })
141        }
142        // AutoField, BigAutoField, and relation fields are handled elsewhere
143        _ => None,
144    }
145}
146
147fn extract_usize_attr(obj: &Bound<'_, PyAny>, attr: &str) -> Option<usize> {
148    obj.getattr(attr).ok()?.extract::<usize>().ok()
149}
150
151fn extract_u32_attr(obj: &Bound<'_, PyAny>, attr: &str) -> Option<u32> {
152    obj.getattr(attr).ok()?.extract::<u32>().ok()
153}
154
155/// Converts a Python object from a Django model instance into a `FieldValue`.
156///
157/// Inspects the `FieldDescriptor` to determine the expected type, then attempts
158/// extraction. Returns `FieldValue::Null` for Python `None`, and `DjangoError`
159/// for type mismatches that cannot be resolved.
160///
161/// # Arguments
162/// * `py_value` - The raw Python object from `getattr(instance, field_name)`.
163/// * `descriptor` - The field descriptor specifying the expected type.
164pub fn convert_python_value_to_field(
165    py_value: &Bound<'_, PyAny>,
166    descriptor: &FieldDescriptor,
167) -> Result<FieldValue, DjangoError> {
168    if py_value.is_none() {
169        return Ok(FieldValue::Null);
170    }
171
172    match &descriptor.field_type {
173        DjangoFieldType::CharField { .. }
174        | DjangoFieldType::TextField
175        | DjangoFieldType::EmailField { .. }
176        | DjangoFieldType::UrlField { .. }
177        | DjangoFieldType::SlugField { .. } => {
178            let val: String = py_value.extract().map_err(|_| DjangoError::TypeConversion {
179                expected: "str".into(),
180                actual: py_value.get_type().qualname().map(|n| n.to_string()).unwrap_or_else(|_| "unknown".into()),
181            })?;
182            Ok(FieldValue::Text(val))
183        }
184
185        DjangoFieldType::IntegerField => {
186            let val: i64 = py_value.extract().map_err(|_| DjangoError::TypeConversion {
187                expected: "int".into(),
188                actual: py_value.get_type().qualname().map(|n| n.to_string()).unwrap_or_else(|_| "unknown".into()),
189            })?;
190            Ok(FieldValue::Integer(val))
191        }
192
193        DjangoFieldType::BigIntegerField => {
194            let val: i64 = py_value.extract().map_err(|_| DjangoError::TypeConversion {
195                expected: "int (i64)".into(),
196                actual: py_value.get_type().qualname().map(|n| n.to_string()).unwrap_or_else(|_| "unknown".into()),
197            })?;
198            Ok(FieldValue::Integer(val))
199        }
200
201        DjangoFieldType::FloatField => {
202            let val: f64 = py_value.extract().map_err(|_| DjangoError::TypeConversion {
203                expected: "float".into(),
204                actual: py_value.get_type().qualname().map(|n| n.to_string()).unwrap_or_else(|_| "unknown".into()),
205            })?;
206            Ok(FieldValue::Float(val))
207        }
208
209        DjangoFieldType::DecimalField { .. } => {
210            // Django's Decimal comes as Python's decimal.Decimal — extract via str to preserve precision
211            let str_val: String = py_value
212                .call_method0("__str__")
213                .and_then(|s| s.extract())
214                .map_err(|_| DjangoError::TypeConversion {
215                    expected: "Decimal (via str)".into(),
216                    actual: py_value.get_type().qualname().map(|n| n.to_string()).unwrap_or_else(|_| "unknown".into()),
217                })?;
218            let decimal = Decimal::from_str(&str_val).map_err(|_| DjangoError::Serialization {
219                field: descriptor.name.clone(),
220                message: format!("cannot parse '{str_val}' as Decimal"),
221            })?;
222            Ok(FieldValue::Decimal(decimal))
223        }
224
225        DjangoFieldType::BooleanField => {
226            let val: bool = py_value.extract().map_err(|_| DjangoError::TypeConversion {
227                expected: "bool".into(),
228                actual: py_value.get_type().qualname().map(|n| n.to_string()).unwrap_or_else(|_| "unknown".into()),
229            })?;
230            Ok(FieldValue::Boolean(val))
231        }
232
233        DjangoFieldType::DateField => {
234            let iso: String = py_value
235                .call_method0("isoformat")
236                .and_then(|s| s.extract())
237                .map_err(|_| DjangoError::TypeConversion {
238                    expected: "date".into(),
239                    actual: py_value.get_type().qualname().map(|n| n.to_string()).unwrap_or_else(|_| "unknown".into()),
240                })?;
241            let date = NaiveDate::parse_from_str(&iso, "%Y-%m-%d").map_err(|_| {
242                DjangoError::Serialization {
243                    field: descriptor.name.clone(),
244                    message: format!("cannot parse '{iso}' as date"),
245                }
246            })?;
247            Ok(FieldValue::Date(date))
248        }
249
250        DjangoFieldType::TimeField => {
251            let iso: String = py_value
252                .call_method0("isoformat")
253                .and_then(|s| s.extract())
254                .map_err(|_| DjangoError::TypeConversion {
255                    expected: "time".into(),
256                    actual: py_value.get_type().qualname().map(|n| n.to_string()).unwrap_or_else(|_| "unknown".into()),
257                })?;
258            let time = NaiveTime::parse_from_str(&iso, "%H:%M:%S%.f").map_err(|_| {
259                DjangoError::Serialization {
260                    field: descriptor.name.clone(),
261                    message: format!("cannot parse '{iso}' as time"),
262                }
263            })?;
264            Ok(FieldValue::Time(time))
265        }
266
267        DjangoFieldType::DateTimeField => {
268            let iso: String = py_value
269                .call_method0("isoformat")
270                .and_then(|s| s.extract())
271                .map_err(|_| DjangoError::TypeConversion {
272                    expected: "datetime".into(),
273                    actual: py_value.get_type().qualname().map(|n| n.to_string()).unwrap_or_else(|_| "unknown".into()),
274                })?;
275            let dt = iso
276                .parse::<DateTime<Utc>>()
277                .or_else(|_| {
278                    // Django may emit naive datetimes without timezone suffix
279                    chrono::NaiveDateTime::parse_from_str(&iso, "%Y-%m-%dT%H:%M:%S%.f")
280                        .map(|naive| naive.and_utc())
281                })
282                .map_err(|_| DjangoError::Serialization {
283                    field: descriptor.name.clone(),
284                    message: format!("cannot parse '{iso}' as datetime"),
285                })?;
286            Ok(FieldValue::DateTime(dt))
287        }
288
289        DjangoFieldType::UuidField => {
290            let str_val: String =
291                py_value
292                    .call_method0("__str__")
293                    .and_then(|s| s.extract())
294                    .map_err(|_| DjangoError::TypeConversion {
295                        expected: "UUID".into(),
296                        actual: py_value.get_type().qualname().map(|n| n.to_string()).unwrap_or_else(|_| "unknown".into()),
297                    })?;
298            let uuid = Uuid::parse_str(&str_val).map_err(|_| DjangoError::Serialization {
299                field: descriptor.name.clone(),
300                message: format!("cannot parse '{str_val}' as UUID"),
301            })?;
302            Ok(FieldValue::Uuid(uuid))
303        }
304
305        DjangoFieldType::JsonField => {
306            // GIL is already held — serialize through Python's json.dumps, then parse in Rust
307            let py = py_value.py();
308            let json_mod = py.import("json").map_err(|e| {
309                DjangoError::Python(format!("cannot import json module: {e}"))
310            })?;
311            let dumped = json_mod
312                .call_method1("dumps", (py_value,))
313                .map_err(|e| DjangoError::Serialization {
314                    field: descriptor.name.clone(),
315                    message: format!("json.dumps failed: {e}"),
316                })?;
317            let json_str: String = dumped.extract::<String>().map_err(|e| {
318                DjangoError::Python(format!("json.dumps returned non-string: {e}"))
319            })?;
320            let value: serde_json::Value =
321                serde_json::from_str(&json_str).map_err(|e| DjangoError::Serialization {
322                    field: descriptor.name.clone(),
323                    message: format!("invalid JSON: {e}"),
324                })?;
325            Ok(FieldValue::Json(value))
326        }
327
328        DjangoFieldType::BinaryField { .. } => {
329            let bytes: Vec<u8> =
330                py_value
331                    .extract()
332                    .map_err(|_| DjangoError::TypeConversion {
333                        expected: "bytes".into(),
334                        actual: py_value.get_type().qualname().map(|n| n.to_string()).unwrap_or_else(|_| "unknown".into()),
335                    })?;
336            Ok(FieldValue::Binary(bytes))
337        }
338    }
339}