I have a class Foo with two fields: an integer field x, and a comma-separated integer field n. The value of n represents the number of elements in x. When I create an instance of this class, I want to set x to a set of n zeroes, each separated by a comma.
Here is the code I have so far:
class Foo(models.Model):
n = models.IntegerField(default=0)
x = models.CommaSeparatedIntegerField(max_length = 100)
def __init__(self, _n):
super(User, self).__init__()
self.n = _n
self.x = [0,] * _n
This seems to work fine to construct instances of Foo. However, when I log in as admin and click on one of the Foo instances, it gives me the error: __init__() takes exactly 2 arguments (4 given)
My interpretation of this is that admin is trying to create an instance of Foo when I ask to view it, without supplying the necessary arguments (although I don't know where the 4 given comes from). So my question is: How should I be writing Python constructors with arguments, such that the instances can also be displayed in the admin page?
One alternative I can think of is to leave the default constructor, and write f = Foo(n = 5) when I create an instance. However, how can I then run the line self.x = [0,] * _n by default without having to write it as a separate function, to call whenever I create an instance?