Skip to content

Commit 0d6b1d7

Browse files
migeed-zmeta-codesync[bot]
authored andcommitted
Collect django pk data at the binding layer
Summary: Collect primary key data from the binding layer. In the next diff, we will propagate it to the class metadata layer. Reviewed By: rchen152 Differential Revision: D85470913 fbshipit-source-id: fdce9485a1ed425674659a946de56353d4b8731a
1 parent 389c3f0 commit 0d6b1d7

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

pyrefly/lib/binding/class.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,9 @@ impl<'a> BindingsBuilder<'a> {
241241
} = &definition
242242
{
243243
self.extract_pydantic_config_dict(e, &name, &mut pydantic_config_dict);
244+
245+
// TODO: We will use this to populate the django metadata
246+
let _is_django_primary_key = self.extract_django_primary_key(e);
244247
}
245248
let (is_initialized_on_class, is_annotated) = match &definition {
246249
ClassFieldDefinition::DefinedInMethod { annotation, .. } => {

pyrefly/lib/binding/django.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
use ruff_python_ast::Expr;
9+
use ruff_python_ast::name::Name;
10+
11+
use crate::binding::bindings::BindingsBuilder;
12+
13+
const PRIMARY_KEY: Name = Name::new_static("primary_key");
14+
15+
impl<'a> BindingsBuilder<'a> {
16+
/// Detect if a field has `primary_key=True` set. This will be used to support Django models with custom primary keys.
17+
pub fn extract_django_primary_key(&self, e: &Expr) -> bool {
18+
let Some(call) = e.as_call_expr() else {
19+
return false;
20+
};
21+
for keyword in &call.arguments.keywords {
22+
if let Some(arg_name) = &keyword.arg
23+
&& arg_name.as_str() == PRIMARY_KEY.as_str()
24+
&& let Expr::BooleanLiteral(bl) = &keyword.value
25+
{
26+
return bl.value;
27+
}
28+
}
29+
30+
false
31+
}
32+
}

pyrefly/lib/binding/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub mod base_class;
99
pub mod binding;
1010
pub mod bindings;
1111
pub mod class;
12+
pub mod django;
1213
pub mod expr;
1314
pub mod function;
1415
pub mod narrow;

0 commit comments

Comments
 (0)