Note that there are some explanatory texts on larger screens.

plurals
  1. POExtending Django ModelForm for Abstract Model with Overriden Save
    primarykey
    data
    text
    <p>So I have this class which is an abstract parent class to a bunch of other classes, let's call it ActivityModel (representing some sort of latest activity/or change done to that object) and I want to override the save method to take another argument every time the object is saved so I define it as follows.</p> <pre><code>class ActivityModel(models.Model): last_updated = models.DateTimeField(auto_now=True) updater = models.ForeignKey(UserProfile) change = models.CharField(max_length=255) def save(self, updater, change, *args, **kwargs): self.updater = updater self.change = change super(ActivityModel, self).save(*args, **kwargs) class Meta: abstract = True </code></pre> <p>But now all the models that I make inherit this class can't use ModelForms because I've changed the save method to require this second field (the UserProfile corresponding to the authenticated user which must be supplied every time it is saved) so I was wondering if it is possible for me to subclass the ModelForm class to override it's save method so that it will call the new ParentModel.save method and fill in the currently logged in user that has been supplied to it. I figure I can just supply the UserProfile on the forms init, but the big question I have is whether it is possible to subclass the ModelForm class and create like a ParentModelForm class which can then be subclassed for each of ParentModel's child classes. Is this possible and if so, how would I go about doing it? </p> <p>All help is very much appreciated! Thanks for your time!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload