Coverage for gws-app/gws/plugin/model_field/related_feature_list/__init__.py: 100%

19 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-05 13:35 +0200

1"""Related feature list field. 

2 

3Represents a parent->child 1:M relationship to another model:: 

4 

5 +--------------+ +-------------+ 

6 | parent table | | child table | 

7 +--------------+ +-------------+ 

8 | fromColumn |-----<<| toColumn | 

9 +--------------+ +-------------+ 

10 

11The value of the field is a list of child features. ``fromColumn`` is the key 

12column in this model's table, by default its primary key; ``toColumn`` is the 

13foreign key column in the child model. 

14 

15The field is implemented as a ``relatedMultiFeatureList`` with a single child 

16model, so it reads and writes the relationship in the same way. Without a 

17configured widget, the field uses a ``featureList`` widget. 

18 

19Example:: 

20 

21 fields+ { 

22 name "pois" 

23 type "relatedFeatureList" 

24 fromColumn "id" 

25 toModel "model_poi" 

26 toColumn "category_id" 

27 widget.type "featureList" 

28 } 

29""" 

30 

31import gws 

32import gws.base.database.model 

33import gws.base.model.related_field as related_field 

34 

35from gws.plugin.model_field import related_multi_feature_list 

36 

37 

38@gws.ext.config.modelField('relatedFeatureList') 

39class Config(related_field.Config): 

40 """Field listing related features in another model.""" 

41 

42 fromColumn: str = '' 

43 """Key column in this table, primary key by default.""" 

44 toModel: str 

45 """UID of the related model.""" 

46 toColumn: str 

47 """Foreign key column in the related model.""" 

48 

49 

50@gws.ext.props.modelField('relatedFeatureList') 

51class Props(related_field.Props): 

52 pass 

53 

54 

55@gws.ext.object.modelField('relatedFeatureList') 

56class Object(related_multi_feature_list.Object): 

57 """Related feature list field object.""" 

58 

59 def configure_relationship(self): 

60 dst_mod = self.get_model(self.cfg('toModel')) 

61 

62 self.rel = related_field.Relationship( 

63 src=related_field.RelRef( 

64 model=self.model, 

65 keyName=self.model.column(self.cfg('fromColumn') or self.model.uidName).name, 

66 ), 

67 dstList=[ 

68 related_field.RelRef( 

69 model=dst_mod, 

70 keyName=dst_mod.column(self.cfg('toColumn')).name, 

71 ) 

72 ], 

73 ) 

74 self.rel.dst = self.rel.dstList[0]