1👍
Correct me if I’m wrong, but I believe your problem is the if
in the following line:
if 'iOS' in record_b['os']:
As a result of this code in every case other than iOS in record_b
, both os_mark_a
and os_mark_b
will be set to 1
. Even if they were set to another value before, they will be overwritten by 1
. To correct this particular problem change the line to:
elif 'iOS' in record_b['os']:
There are other things I find weird:
-
In most cases you set just one of
os_mark_b
oros_mark_a
, leaving
the other one unset. You could correct this for example by settings
both to a default value first. -
You don’t need to load the values to a dictionary using
values()
– just use.get()
and use the resulting object directly. Among other benefits this would eliminate lines 6, 7, 10, 11, 12, 13 completely. -
Calculations for A and B seem completely separate. It seems it would be wiser to create a smaller method that calucaltes mark for a single object.
-
Your method doesn’t return anything. I presume that’s only because it’s a simplified example.
-
What’s the following code is about? That doesn’t do anything…
mobile_a = mobile_a mobile_b = mobile_b
Update: Example of code without those issues:
def calculateMark(mobile):
record = TechSpecificationAdd.objects.get(mobile_name=mobile)
if 'Android' in record.os:
return 8.9
if 'iOS' in record.os:
return 14
return 1
Update 2: Also, since we already got so far in making this code better – this shouldn’t be a separate function, but a method on a TechSpecificationAdd
object.
class TechSpecificationAdd(models.Model):
# some fields
def calculateMark(self):
if 'Android' in self.os:
return 8.9
if 'iOS' in self.os:
return 14
return 1
You would then use this in your code like this:
record = TechSpecificationAdd.objects.get(mobile_name=mobile)
mark = record.calculateMark()