Reputation: 519
override save method in a form, I have an image that needs to be saved in a different database, but the rest of the fields should go to the default one, also I should store the image id in the default database
class TbUserRegisterForm(CustomUserCreationForm):
email = forms.EmailField()
image = forms.ImageField()
class Meta:
model = TbUser
fields = ['username', 'image', 'email', 'sex', 'role', 'department', 'password1', 'password2']
Upvotes: 0
Views: 1183
Reputation: 2380
This is How I usually overwrite save method in model form:
def save(self, commit=True):
# your logic or Save your object for example:
obj = Model.objects.create(...)
return obj
Or you can also do this:
def save(self, commit=True):
obj = super().save(commit=False)
# do you logic here for example:
obj.field = something
if commit:
# Saving your obj
obj.save()
return obj
Upvotes: 3