TypeError unsupported operand type(s) for *: 'NoneType' and 'int'

187 views
Skip to first unread message

omar ahmed

unread,
Apr 6, 2019, 1:35:52 PM4/6/19
to Django users
models.py
class Club(models.Model):
league_names = models.ForeignKey(League, on_delete= models.CASCADE, related_name='club')
name = models.CharField(max_length=100)
logo = models.ImageField(upload_to='media/core', max_length=255, null=True, blank=True)
won = models.IntegerField()
draw = models.IntegerField()
lost = models.IntegerField()
goal_for = models.IntegerField()
goal_against = models.IntegerField()
club_position = models.IntegerField()

def CalcPoints(self):
return self.won*3 + self.draw


admin.py

class ClubAdmin(admin.ModelAdmin):
list_display = ['league_names', 'name', 'logo', 'won', 'draw', 'lost', 'total_points', 'goal_for', 'goal_against', 'goal_diff']
readonly_fields = ('total_points', 'goal_diff',)

admin.site.register(Club, ClubAdmin)

total_points = property(CalcPoints)

def GoalDiff(self):
return self.goal_for - self.goal_against

goal_diff = property(GoalDiff)


def __str__(self):
return self.name

Roger Gammans

unread,
Apr 7, 2019, 5:58:50 AM4/7/19
to django...@googlegroups.com
Omar,

When posting question to the list it's good practice to practice tell us a little bit about what your trying to do, and how what
actions cause the error to occur.

In this case though you only have a (single line in CalcPoints) which uses the '*' operator.

On Sat, 2019-04-06 at 10:35 -0700, omar ahmed wrote:
models.py
def CalcPoints(self):
return self.won*3 + self.draw


The python documentation has this to say about the '*' operator

The * (multiplication) operator yields the product of its arguments. The arguments must either both be numbers, or one argument must be an integer and the other must be a sequence. In the former case, the numbers are converted to a common type and then multiplied together. In the latter case, sequence repetition is performed; a negative repetition factor yields an empty sequence.

Since you've provided a literal integer ( the '3') as one of your arguments, we can deduce the None value
is in self.won. Since you have 'declared' self.won as an IntegerField; we can see that as in the
normal case we should satisfy the first clause of the reference, "arguments must either both be numbers"
however so some reason self.won is None in your case.

Without seeing the rest of your code it's impossible to guess accurately but my instinct is your haven't
initialised your instance from the database (or set a won value when creating the object).

If you expected to have a default value you need to set one where you set up self.won see

HTH,

-- 
Roger Gammans <rgam...@gammascience.co.uk>

omar ahmed

unread,
Apr 7, 2019, 12:08:50 PM4/7/19
to Django users
ok i understood
thanks for help
Reply all
Reply to author
Forward
0 new messages