#9758: modelformset_factory forms generate error when using queryset
-----------------------------------+----------------------------------------
 Reporter:  [EMAIL PROTECTED]  |       Owner:  nobody    
   Status:  new                    |   Milestone:            
Component:  Forms                  |     Version:  1.0       
 Keywords:                         |       Stage:  Unreviewed
Has_patch:  0                      |  
-----------------------------------+----------------------------------------
 What I'm trying to do:

   * I'm building a formset with exactly seven entries, one for each day of
 the last week.
   * I use modelformset_factory to create my form and specify a queryset to
 filter the particular item and date range.

 What I see:

   * The page displays the seven entries as expected. The first seven days
 are created fine and can be edited at any point.
   * If I try to edit additional entries, I see the following error:

 (Hidden field id) Status report with this None already exists.

   * The kicker is that if I remove the queryset limitation, this error
 goes away. Ie, my formset includes all entries, not just the seven I want.
   * Without the queryset, I can add / edit as many items and weeks as I
 want.

 Here's a minimal example, which assumes you already have data stored in
 StatusReport.

 {{{
 class StatusReport(models.Model):
    item          = models.IntegerField()
    report_date   = models.DateField()
    status        = models.CharField(max_length=10)
 }}}

 view:

 {{{
 def detail(request, item_id):

     # removed sanity checks, entry creation, etc

     StatusFormSet = modelformset_factory(StatusReport, extra=0)
     if request.method == 'POST':
         formset = StatusFormSet(request.POST)
         if formset.is_valid():
             formset.save()
     else:
         ### with error
         #limited_reports = StatusReport.objects.filter(item=item_id)
         #formset = StatusFormSet(queryset=limited_reports)
         ### without error
         formset = StatusFormSet()
     return render_to_response('manage_items.html',{'formset': formset})
 }}}

 and generic template
 {{{
 <form method="POST" action="">
     <table>
         {{ formset }}
     </table>
     <input type = "submit" value = "Submit">
 </form>
 }}}

-- 
Ticket URL: <http://code.djangoproject.com/ticket/9758>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to