Im trying to make some kind of logic function " sufficient_information_provided " which returns boolean. As for now im new to python syntax.. i would appreciate if anyone could help figure this out, here is the model in django app
class FamilyMember(models.Model):
transaction = models.ForeignKey(Transaction, on_delete=models.CASCADE)
family_group = models.ForeignKey(FamilyGroup,
on_delete=models.CASCADE,
null=True,
blank=True)
name = models.CharField(max_length=100, null=True, blank=True)
date_of_birth = models.DateField(null=True, blank=True)
relationship = models.ForeignKey(Relationship, on_delete=models.PROTECT)
dependant_child_age_range = models.ForeignKey(DependantChildAgeRange,
null=True,
blank=True,
on_delete=models.PROTECT)
care_percentage = models.PositiveSmallIntegerField(
null=True, blank=True, validators=[
MaxValueValidator(100),
]
)
income = models.DecimalField(max_digits=6,
decimal_places=2,
null=True,
blank=True)
And here is the function in same .py folder
@property
def sufficient_information_provided(self):
b = ('Tenant', 'Partner')
if (
self.name and
self.date_of_birth and
self.relationship and(
(
self.relationship.name not in b and
self.dependant_child_age_range
) or (
self.relationship.name in b and
self.income
)
)
):
return True
return False
What i'm trying to do is:
relationship.name
not in the tuple and dependant_age_range
is not None
relationship.name
is in the tuple and self.income
is not None
It returns True
else returns False
.
For code clarity, you can use a combination of all
and any
:
all
will return True
if all elements of the iterable return True
. any
will return True
if at least one element of the iterable returns True
.So your function – I just copied the algorithm without questioning it - could look like:
def sufficient_information_provided(self):
b = ["Tenant", "Partner"]
return all(
[
self.name,
self.date_of_birth,
self.relationship,
any(
[
(
self.relationship.name not in b
and self.dependant_child_age_range
),
(self.relationship.name in b and self.income),
]
),
]
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.