@@ -34,7 +34,10 @@ class SchoolWithStudentsFactory(SchoolFactory):
34
34
@factory .post_generation
35
35
def students (obj , create , extracted , ** kwargs ):
36
36
if create :
37
- students = extracted or StudentFactory .create_batch (kwargs .get ('count' , 5 ))
37
+ students = extracted or StudentFactory .create_batch (
38
+ kwargs .pop ('count' , 5 ),
39
+ ** kwargs
40
+ )
38
41
obj .students .set (students )
39
42
return students
40
43
@@ -58,31 +61,19 @@ class Meta:
58
61
model = Roster
59
62
60
63
student = factory .SubFactory (StudentFactory )
61
- # Make sure the student and the school_course are from the same school
64
+
65
+ # NOTE: You can use `factory.SelfAttribute` for the below attributes.
66
+ # We prefer using `factory.LazyAttribute` as we find the definition more explicit.
62
67
school_course = factory .SubFactory (
63
68
SchoolCourseFactory ,
64
- school = factory .SelfAttribute ('..student.school' )
65
- )
66
- # The `SelfAttribute` is just an interface that uses LazyAttribute under the hood:
67
- # school_course = factory.SubFactory(
68
- # SchoolCourseFactory,
69
- # school=factory.LazyAttribute(lambda course: course.factory_parent.student.school)
70
- # )
71
-
72
- start_date = factory .SelfAttribute ('school_course.start_date' )
73
- end_date = factory .SelfAttribute ('school_course.end_date' )
74
-
75
- active = True
76
- # We'd recommend you using LazyAttribute instead of Maybe as it's more explicit
77
- deactivated_at = factory .Maybe (
78
- 'active' ,
79
- no_declaration = factory .LazyAttribute (lambda self : faker .date_between_dates (
80
- date_start = self .start_date ,
81
- date_end = self .end_date
82
- )),
83
- yes_declaration = None
69
+ school = factory .LazyAttribute (
70
+ lambda course : course .factory_parent .student .school
71
+ )
84
72
)
85
73
74
+ start_date = factory .LazyAttribute (lambda _self : _self .school_course .start_date )
75
+ end_date = factory .LazyAttribute (lambda _self : _self .school_course .end_date )
76
+
86
77
87
78
def get_future_roster_start_date (roster_obj ):
88
79
if not roster_obj .start_after :
@@ -109,7 +100,8 @@ class SchoolCourseWithRostersFactory(SchoolCourseFactory):
109
100
def rosters (obj , create , extracted , ** kwargs ):
110
101
if create :
111
102
rosters = extracted or RosterFactory .create_batch (
112
- kwargs .get ('count' , 5 ),
103
+ kwargs .pop ('count' , 5 ),
104
+ ** kwargs ,
113
105
student__school = obj .school # NOTE!
114
106
)
115
107
0 commit comments