I am just learning Python and Django and trying to understand how objects are initialized. Suppose I have a Django model called Alpha, with three attributes: x, y and z. x and y are integers, and z is a boolean. When I create an Alpha object, I want to initialize x and y (similar to a constructor in C++). Then, I want to automatically set z to True if x is equal to y, and False otherwise. I have been recommended to edit the __init__ function, and I have tried the following:
from django.db import models
class Alpha:
x = models.IntegerField(default = 0)
y = models.IntegerField(default = 0)
z = models.BooleanField(default = 'false')
However, this does not work as I would expect. In the Django shell, I have the following:
>>> from test.models import Alpha
>>> a = Alpha(3, 3)
>>> a.x
3
>>> a.y
0
>>> a.z
u'false'
I am trying to set x=3 and y=3, therefore z='True', but this is obviously not what is happening. y is not set to anything, and I suspect this is because the first argument is actually set to self, rather than x. I don't think I quite understand the role of the self argument, which may be the problem...
Any advice on how I should be doing this properly?
__init__code? Btw: it should beFalse(a boolean) not'false'(a string).