Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango How can I initial the object ID of all objects in queryset in a formset hidden field?
    primarykey
    data
    text
    <p>I'm trying to create a simple inbox application that allow users to send each other message using the UserModel. </p> <p>How can I populate id of all objects in a queryset in a hidden field of a formset? Because when I try</p> <pre><code>File "C:\mysite\pet\views.py" in message 913. initial.update({'hidden_field': messages.id}) </code></pre> <p>Exception Type: AttributeError at /message/ Exception Value: 'function' object has no attribute 'id'</p> <pre><code>class Message(models.Model): user = models.ForeignKey(User, related_name='sender') recipient = models.ForeignKey(User, related_name='recipient') created = models.DateTimeField(auto_now_add=True) body = models.CharField(max_length=1000) read = models.BooleanField(default=False) trash = models.BooleanField(default=False) sentmessage = models.BooleanField(default=False) thread = models.ForeignKey(Thread,blank=True,null=True) draft = models.BooleanField(default=False) def __unicode__(self): return self.body </code></pre> <p>views</p> <pre><code>@login_required def message(request): person = Person.objects.get(user=request.user) messages = Message.objects.filter(recipient=request.user.id).order_by("-created").values initial = {} initial.update({'hidden_field': messages.id}) form = formset_factory(CheckBoxForm) formset = form(initial=initial) return render(request,'messages.html',{'messages':messages,'form':form,'person':person}) </code></pre> <p>forms</p> <pre><code>class CheckBoxForm(forms.Form): checkbox = forms.BooleanField(required=False) hidden_field = forms.CharField(widget=forms.HiddenInput()) </code></pre> <p>template</p> <pre><code> {% for m in messages %} {{m.body}} {% endfor %} </code></pre> <p>@ new code</p> <pre><code> @login_required def message(request): messages = Message.objects.filter(recipient=request.user).order_by("-created") form = CheckBoxForm() if request.method =='POST': form = ReplyForm(request.POST) if form.is_valid(): id = request.POST.get('hidden_field', False) Message.objects.filter(pk=id).delete() return render(request,'messages.html', {'messages':messages,'form':form,'person':person}) </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    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