I have a model with an __init__ method:
class Foo(models.Model):
name = models.CharField(max_length=50)
def __init__(self, *args, **kwargs):
self.bar = kwargs.pop('bar', False)
super(Foo, self).__init__(*args, **kwargs)
if self.bar:
# do something
pass
Now, i need to create a specific ModelForm:
class FooForm(ModelForm):
class Meta:
model = Foo(bar='something')
fields = ('name',)
That does not work apparently:
TypeError: 'Foo' object is not callable
Is there any way i can overcome this?
Update
More information on what i want to achieve: I have an Image model with an ImageField. It has different storage methods depending on the form that uses it.
The model:
class Image(models.Model):
image = models.ImageField(upload_to=imageUploadTo)
user = models.ForeignKey('auth.User')
def __init__(self, *args, **kwargs):
self.overwrite = kwargs.pop('overwrite', False)
super(Image, self).__init__(*args, **kwargs)
if self.overwrite:
self.image.storage = OverwriteStorage()
Now i want to be able to create forms that overwrite the old image and forms that use the default behavior. What’s the best way to achieve this?
No, that’s not how it works at all, and this has nothing to do with your custom init. You don’t call things inside Meta. In your case, you pass the parameter when you initialize the form in your view.