I didn't alter the model but the view def new(request, form_class=BlogForm, template_name="blog/new.html"): if request.method == "POST": if request.POST["action"] == "create": blog_form = form_class(request.user, request.POST) if blog_form.is_valid(): blog = blog_form.save(commit=False) blog.author = request.user if getattr(settings, 'BEHIND_PROXY', False): blog.creator_ip = request.META ["HTTP_X_FORWARDED_FOR"] else: blog.creator_ip = request.META['REMOTE_ADDR'] blog.save(commit=False) request.user.message_set.create(message=_ ("Successfully saved post '%s'") % blog.item) if notification: if blog.status == 2: # published if friends: # @@@ might be worth having a shortcut for sending to all friends notification.send((x['friend'] for x in Friendship.objects.friends_for_user(blog.author)), "blog_friend_post", {"post": blog}) return HttpResponseRedirect(reverse ("blog_list_yours")) else: blog_form = form_class() else: blog_form = form_class(request.user) return render_to_response(template_name, { "blog_form": blog_form, }, context_instance=RequestContext(request))
There is no save in the form: class BlogForm(forms.ModelForm): user = 'mark' slug = forms.SlugField(max_length=20, help_text = _("unique name"), error_message = _("This value must contain only letters, numbers, underscores and hyphens.")) imagepost = forms.ModelChoiceField(queryset=Image.objects.filter (member__username=user)) class Meta: model = Post exclude = ('author', 'creator_ip', 'created_at', 'updated_at', 'publish') def __init__(self, user=None, *args, **kwargs): self.user = user super(BlogForm, self).__init__(*args, **kwargs) self.fields['imagepost'].queryset = Image.objects.filter (member=self.user) def clean_slug(self): if not self.instance.pk: if Post.objects.filter(author=self.user, created_at__month=datetime.now().month, created_at__year=datetime.now ().year, slug=self.cleaned_data['slug']).count(): raise forms.ValidationError(u'This field must be unique for username, year, and month') return self.cleaned_data['slug'] try: post = Post.objects.get(author=self.user, created_at__month=self.instance.created_at.month, created_at__year=self.instance.created_at.year, slug=self.cleaned_data ['slug']) if post != self.instance: raise forms.ValidationError(u'This field must be unique for username, year, and month') except Post.DoesNotExist: pass return self.cleaned_data['slug'] This resulted in the TypeError On Jan 18, 7:33 pm, Adam Knight <a...@hopelessgeek.com> wrote: > Ensure it's the save() method on the FORM and not the MODEL. :) > > > > > > On Mon, Jan 18, 2010 at 12:23 PM, GoSantoni <mj.schuur...@gmail.com> wrote: > > Hi Nek4life, > > > Unfortunately that didn't work. > > > TypeError at /blog/new/ > > save() got an unexpected keyword argument 'commit' > > Request Method: POST > > Request URL: http://localhost:8000/blog/new/ > > Exception Type: TypeError > > Exception Value: > > save() got an unexpected keyword argument 'commit' > > > On Jan 18, 4:21 pm, nek4life <nek4l...@gmail.com> wrote: > >> On Jan 18, 10:01 am, GoSantoni <mj.schuur...@gmail.com> wrote: > > >> > Hi > > >> > Trying to use save_m2m as described > >> > herehttp://docs.djangoproject.com/en/1.0/topics/forms/modelforms/#the-sav... > >> > Though in run into an attrribute error 'Post' object has no attribute > >> > 'save_m2m' > > >> > *** Model *** > >> > class Post(models.Model): > >> > id = models.AutoField(primary_key=True) > >> > imagepost = models.ManyToManyField(Image, verbose_name=_ > >> > ('media')) > > >> > def save(self, force_insert=False, force_update=False): > >> > self.updated_at = datetime.now() > >> > super(Post, self).save(force_insert, force_update) > > >> > *** View *** > >> > def new(request, form_class=BlogForm, template_name="blog/new.html"): > >> > if request.method == "POST": > >> > if request.POST["action"] == "create": > >> > blog_form = form_class(request.user, request.POST) > >> > if blog_form.is_valid(): > >> > blog = blog_form.save(commit=False) > >> > blog.author = request.user > >> > if getattr(settings, 'BEHIND_PROXY', False): > >> > blog.creator_ip = request.META > >> > ["HTTP_X_FORWARDED_FOR"] > >> > else: > >> > blog.creator_ip = request.META['REMOTE_ADDR'] > >> > blog.save() > >> > blog.save_m2m() > >> > request.user.message_set.create(message=_ > >> > ("Successfully saved post '%s'") % blog.item) > >> > if notification: > >> > if blog.status == 2: # published > >> > if friends: # @@@ might be worth having a > >> > shortcut for sending to all friends > >> > notification.send((x['friend'] for x in > >> > Friendship.objects.friends_for_user(blog.author)), "blog_friend_post", > >> > {"post": blog}) > > >> > return HttpResponseRedirect(reverse > >> > ("blog_list_yours")) > >> > else: > >> > blog_form = form_class() > >> > else: > >> > blog_form = form_class(request.user) > > >> > return render_to_response(template_name, { > >> > "blog_form": blog_form, > > >> > }, context_instance=RequestContext(request)) > > >> > Question 1: > >> > So far i found these 2 posts using map and instances in the view. Is > >> > that the way to solve > >> > this?http://stackoverflow.com/questions/1477319/how-to-get-the-django-curr... > > >> > Question 2: > >> > Does the def save() in the model need alteration? Or addition of a def > >> > save_m2m() ? Change gives a 'super' object has no attribute 'save_m2m' > > >> > Thanks > > >> From the link you provided. > > >> "[..] To work around this problem, every time you save a form using > >> commit=False, Django adds a save_m2m() method to your ModelForm > >> subclass." > > >> Try adding blog.save(commit=False) instead of just blog.save() > > > -- > > You received this message because you are subscribed to the Google Groups > > "Django users" group. > > To post to this group, send email to django-us...@googlegroups.com. > > To unsubscribe from this group, send email to > > django-users+unsubscr...@googlegroups.com. > > For more options, visit this group > > athttp://groups.google.com/group/django-users?hl=en. > > -- > Adam
-- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.