Well I have it configured now so that the fields are editable but that
when None a default value gets set, either the author=request.user
(admin.py layer) and pub_date=datetime.now() when no value is
specified. I had to set (null=True, blank=True) for author in order
to get past form field validation. Then I had to also remove my own
validation from the clean method (and move it to save) for:
if not self.author:
raise ValidationError('New Announcements require an Author
be specified!')
Because it is also a requirement that every Announcement have an
author. When I left this same code in the clean method, my saves
would always fail until I specified an author in (advanced options)
but now it can pass clean and still be validated against. I still
have an issue considering how to let people use the admin in a real
world situation. Is it even really desirable to have the option to
edit who the Announcement is published_by? Well in one sense yes
because we work as teams, But entirely no also. for security. You
could add a last_modified field to set editable=False to always keep
an eye on who last changed it, but even still a complete list would be
better. Better yet would be a system where folk didn't pull pranks
like that. Well I guess admin has history and I should perhaps
eventually create history in my own custom form processing if that
kind of monitoring is so important.
Thanks again,
Matt
On Feb 17, 1:36 am, Chris Matthews <[email protected]> wrote:
> Hi Matteius,
>
> You must set blank=True in the model so that the form can be returned to you
> with those fields empty and then you fill it in the save().
>
> pub_date = models.DateTimeField(blank=True)
> author = models.ForeignKey(User, blank=True, verbose_name='Author')
>
> Also read up on editable=False
>
> Regards
> Chris
>
>
>
> -----Original Message-----
> From: [email protected] [mailto:[email protected]] On
> Behalf Of Matteius
> Sent: 17 February 2011 08:47
> To: Django users
> Subject: How to Auto fill fields on Save in Django Admin?
>
> I have an Announcement model that has two fields I want to auto-fill
> in when they are left blank (they are required fields with normal
> defaults). I have the following Code (simplified data listing for
> this post) ---- THe Problem is that when I go to save an Announement
> in the admin with either of those fields blank it returns to the Add/
> Edit Form with "This field is required." despite my code to try and
> fill that in. Clearly I'm missing how to do this, any advice?
>
> class Announcement(models.Model):
> """ Represents an Announcement in the classcomm system. """
>
> # Data Model Fields
> pub_date = models.DateTimeField()
> author = models.ForeignKey(User, verbose_name='Author')
>
> def clean(self):
> """ Implement auto fill pub_date. """
> if not self.pub_date:
> self.pub_date = datetime.today()
> super(Announcement, self).clean()
> # EndDef
>
> def save(self):
> """ Implement auto fill pub_date. """
> if not self.pub_date:
> self.pub_date = datetime.today()
> super(Announcement, self).save()
> # EndDef
> # EndClass
>
> Then in admin.py I have:
>
> class AnnouncementAdmin(admin.ModelAdmin):
> """ Admin customizations for Announcement Models. """
> fieldsets = (
> (None, {
> 'fields': ('headline', 'department', 'course', 'content')
> }),
> ('Advanced options', {
> 'classes': ('collapse',),
> 'fields': ('author', 'pub_date', 'make_global')
> }),
> )
>
> def save_model(self, request, obj, form, change):
> """ Autofill in author when blank on save models. """
> obj.author = request.user
> obj.save()
> # EndDef
>
> def save_formset(self, request, form, formset, change):
> """ Autofill in author when blank on save formsets. """
> instances = formset.save(commit=False)
> for instance in instances:
> instance.author = request.user
> instance.save()
> formset.save_m2m()
> # EndDef
> # EndClass
>
> Regards,
> Matt
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group
> athttp://groups.google.com/group/django-users?hl=en.
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.