Salva
Salva

Reputation: 37

Django: How to custom save a model/form based on a non-persistent field of a form?

I'm reposting this question since I asked 2 in 1 in a previous post.

I want to set the field publication_date in the Entry model to change when the boolean to_publish is set to true, something like the custom save() method in the model, but I haven't been able to do that on the form.

#blog/models.py
class Entry(models.Model):
    title = models.CharField(max_length=100)
    body = models.TextField()
    slug = models.SlugField(max_length=100, unique=True)
    creation_date = models.DateTimeField(editable=False)
    publication_date = models.DateTimeField(editable=False, null=True)
    modification_date = models.DateTimeField(editable=False)

    author = models.ForeignKey(User)
    categories = models.ManyToManyField(Category)
    tags = models.ManyToManyField(Tag)

    objects = EntryQuerySet.as_manager()

    def __str__(self):
        return self.title

    def was_published_recently(self):
        now = datetime.datetime.now()
        return now - datetime.timedelta(days=1) <= self.publication_date <= now

    def is_published(self):
        return self.publication_date is not False

    is_published.boolean = True
    is_published.short_description = 'Is it Published?'

    def get_absolute_url(self):
        return reverse("entry_detail", kwargs={"slug": self.slug})

    class Meta():
        verbose_name = "Blog Entry"
        verbose_name_plural = "Blog Entries"
        ordering = ['-creation_date']

    def save(self, *args, **kwargs):
        """ On save, update timestamps """
        if not self.id:
            self.creation_date = datetime.datetime.now()
        self.modification_date = datetime.datetime.now()
        return super(Entry, self).save(*args, **kwargs)

#blog/forms.py
class EntryAdminForm(forms.ModelForm):
    to_publish = forms.BooleanField(required=False)

    class Meta:
        model = Entry
        fields = ['title', 'body', 'slug', 'author', 'categories', 'tags']

#blog/admin.py
class EntryAdmin(MarkdownModelAdmin):
    form = EntryAdminForm
    list_display = ("title", "author", "creation_date", "publication_date", "is_published")
    prepopulated_fields = {"slug": ("title",)}
    formfield_overrides = {TextField: {'widget': AdminMarkdownWidget}}

Thank you.

Upvotes: 1

Views: 1644

Answers (1)

catavaran
catavaran

Reputation: 45575

Override the save_model() method of the EntryAdmin:

class EntryAdmin(MarkdownModelAdmin):
    ...
    def save_model(self, request, obj, form, change):
        if form.cleaned_data.get('to_publish'):
            obj.publication_date = datetime.now()
        obj.save()

The other (and more complex) option is to override the EntryAdminForm.save() method:

class EntryAdminForm(forms.ModelForm):
    ....
    def save(self, *args, **kwargs):
        entry = super(EntryAdminForm, self).save(*args, **kwargs)
        if self.cleaned_data.get('to_publish'):
            entry.publication_date = datetime.now()
            if kwargs.get('commit', True):
                entry.save()
        return entry

Upvotes: 2

Related Questions