Version 1 (modified by 17 years ago) ( diff ) | ,
---|
How to set the current on the model instance using newforms admin.
from django.db import models from django.http import Http404, HttpResponse, HttpResponseRedirect from django.utils.html import escape from django.utils.safestring import mark_safe from django.db import models, transaction from django.contrib.auth.models import User from django.utils.encoding import force_unicode from django.utils.translation import ugettext as _ class Post(models.Model): user = models.ForeignKey(User) content = models.TextField() from django.contrib import admin class PostModelAdmin(admin.ModelAdmin): fields= ('content',) def __call__(self, request, url): self.user = request.user return super(PostModelAdmin, self).__call__(request, url) def save_add(self, request, model, form, formsets, post_url_continue): """ Saves the object in the "add" stage and returns an HttpResponseRedirect. `form` is a bound Form instance that's verified to be valid. """ from django.contrib.admin.models import LogEntry, ADDITION from django.contrib.contenttypes.models import ContentType opts = model._meta new_object = form.save(commit=False) new_object.user = self.user new_object.save() form.save_m2m() if formsets: for formset in formsets: # HACK: it seems like the parent obejct should be passed into # a method of something, not just set as an attribute formset.instance = new_object formset.save() pk_value = new_object._get_pk_val() LogEntry.objects.log_action(request.user.id, ContentType.objects.get_for_model(model).id, pk_value, force_unicode(new_object), ADDITION) msg = _('The %(name)s "%(obj)s" was added successfully.') % {'name': opts.verbose_name, 'obj': new_object} # Here, we distinguish between different save types by checking for # the presence of keys in request.POST. if request.POST.has_key("_continue"): request.user.message_set.create(message=msg + ' ' + _("You may edit it again below.")) if request.POST.has_key("_popup"): post_url_continue += "?_popup=1" return HttpResponseRedirect(post_url_continue % pk_value) if request.POST.has_key("_popup"): return HttpResponse('<script type="text/javascript">opener.dismissAddAnotherPopup(window, "%s", "%s");</script>' % \ # escape() calls force_unicode. (escape(pk_value), escape(new_object))) elif request.POST.has_key("_addanother"): request.user.message_set.create(message=msg + ' ' + (_("You may add another %s below.") % opts.verbose_name)) return HttpResponseRedirect(request.path) else: request.user.message_set.create(message=msg) # Figure out where to redirect. If the user has change permission, # redirect to the change-list page for this object. Otherwise, # redirect to the admin index. if self.has_change_permission(request, None): post_url = '../' else: post_url = '../../../' return HttpResponseRedirect(post_url) save_add = transaction.commit_on_success(save_add) def save_change(self, request, model, form, formsets=None): """ Saves the object in the "change" stage and returns an HttpResponseRedirect. `form` is a bound Form instance that's verified to be valid. `formsets` is a sequence of InlineFormSet instances that are verified to be valid. """ from django.contrib.admin.models import LogEntry, CHANGE from django.contrib.contenttypes.models import ContentType opts = model._meta new_object = form.save(commit=False) new_object.user = self.user new_object.save() form.save_m2m() pk_value = new_object._get_pk_val() if formsets: for formset in formsets: formset.save() # Construct the change message. TODO: Temporarily commented-out, # as manipulator object doesn't exist anymore, and we don't yet # have a way to get fields_added, fields_changed, fields_deleted. change_message = [] #if manipulator.fields_added: #change_message.append(_('Added %s.') % get_text_list(manipulator.fields_added, _('and'))) #if manipulator.fields_changed: #change_message.append(_('Changed %s.') % get_text_list(manipulator.fields_changed, _('and'))) #if manipulator.fields_deleted: #change_message.append(_('Deleted %s.') % get_text_list(manipulator.fields_deleted, _('and'))) #change_message = ' '.join(change_message) if not change_message: change_message = _('No fields changed.') LogEntry.objects.log_action(request.user.id, ContentType.objects.get_for_model(model).id, pk_value, force_unicode(new_object), CHANGE, change_message) msg = _('The %(name)s "%(obj)s" was changed successfully.') % {'name': opts.verbose_name, 'obj': new_object} if request.POST.has_key("_continue"): request.user.message_set.create(message=msg + ' ' + _("You may edit it again below.")) if request.REQUEST.has_key('_popup'): return HttpResponseRedirect(request.path + "?_popup=1") else: return HttpResponseRedirect(request.path) elif request.POST.has_key("_saveasnew"): request.user.message_set.create(message=_('The %(name)s "%(obj)s" was added successfully. You may edit it again below.') % {'name': opts.verbose_name, 'obj': new_object}) return HttpResponseRedirect("../%s/" % pk_value) elif request.POST.has_key("_addanother"): request.user.message_set.create(message=msg + ' ' + (_("You may add another %s below.") % opts.verbose_name)) return HttpResponseRedirect("../add/") else: request.user.message_set.create(message=msg) return HttpResponseRedirect("../") save_change = transaction.commit_on_success(save_change) admin.site.register(Post, PostModelAdmin)
Note:
See TracWiki
for help on using the wiki.