Re: modelformset_factory error: (Hidden field id) with this None already exists.

2008-12-08 Thread cyberjack

Thanks for the idea Karen,

 What's the right mechanism for passing the queryset back into the
form? I don't see an example in the docs.

Thanks!

-Josh

On Dec 4, 4:10 pm, "Karen Tracey" <[EMAIL PROTECTED]> wrote:
> On Thu, Dec 4, 2008 at 2:12 AM, cyberjack <[EMAIL PROTECTED]> wrote:
>
> > Hi,
>
> >  I haven't been able to solve this problem, but have made some
> > progress:
>
> > It's definitely *not* bug 9039. I've download the tests from that
> > ticket and confirmed the fix was included in 1.0.2 final.
>
> > The problem is related to using querysets with formset_factory.  Ie,
> > if I include a queryset filter, then I see this error. If I don't
> > include a queryset, and thus edit all status reports, I don't see the
> > error.
>
> > Lastly, I've simplified my code producing the problem:
>
> > my model:
>
> > class StatusReport(models.Model):
> >   vehicle       = models.IntegerField()
> >   report_date   = models.DateField()
> >   status        = models.CharField(max_length=10)
>
> > and my view:
>
> > def detail(request, vehicle_id):
> >    limited_reports = StatusReport.objects.filter(vehicle=vehicle_id)
>
> >    StatusFormSet = modelformset_factory(StatusReport, extra=0)
> >    if request.method == 'POST':
> >        formset = StatusFormSet(request.POST)
> >         if formset.is_valid():
> >            formset.save()
> >     else:
> >        # with error
> >        #formset = StatusFormSet(queryset=limited_reports)
> >        # without error
> >        formset = StatusFormSet()
> >    return render_to_response('manage_articles.html',
> >                              {'formset': formset,
> >                               'vehicle_id': vehicle_id})
>
> > Is this a bug in Django? If so, what should I include in the bug
> > report?
>
> I think the bug is that you need to pass the queryset parameter when you are
> dealing with a POST (if you've specified it on the GET) so that the posted
> data can be matched up to the existing model instances.  Otherwise I think
> it will be trying to validate and save as all new instances, and I think the
> error message is saying you wind up with duplicate primary key ids when you
> try that (though I will say that error message might could use some
> improvement).
>
> Karen
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@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-users?hl=en
-~--~~~~--~~--~--~---



Re: modelformset_factory error: (Hidden field id) with this None already exists.

2008-12-03 Thread cyberjack

Hi,

 I haven't been able to solve this problem, but have made some
progress:

It's definitely *not* bug 9039. I've download the tests from that
ticket and confirmed the fix was included in 1.0.2 final.

The problem is related to using querysets with formset_factory.  Ie,
if I include a queryset filter, then I see this error. If I don't
include a queryset, and thus edit all status reports, I don't see the
error.

Lastly, I've simplified my code producing the problem:

my model:

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

and my view:

def detail(request, vehicle_id):
limited_reports = StatusReport.objects.filter(vehicle=vehicle_id)

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

Is this a bug in Django? If so, what should I include in the bug
report?

Thanks,

-Josh


On Dec 2, 3:49 pm, cyberjack <[EMAIL PROTECTED]> wrote:
> Hi all, I've been stumped by this error for the past few days and
> haven't made any progress. Could someone please help out?
>
>   I've created a formset of seven entries, one for each day of the
> previous week. The first submission works fine; all seven entries are
> successfully submitted. Subsequent submissions, however, generate the
> following error for each of the seven entries:
>
>  (Hidden field id) Status report with this None already exists.
>
> I found an django issue from a few months ago which might be related
> [1]. However, this bug was resolved back in Oct. and I'm running
> 1.0.2, so I assume the CL is included. Is there any way to be
> certain?
>
> Here are my models, simplified for space:
>   class Vehicle(models.Model):
>     number          = models.IntegerField(unique=True)
>
>   class StatusReport(models.Model):
>     date      = models.DateField()
>     vehicle   = models.ForeignKey(Vehicle, to_field='number')
>     status    = models.CharField
> ('status',max_length=8,choices=STATUS_REPORT_CHOICES)
>
>     class Meta:
>       unique_together = ("date", "vehicle")
>
> And here's my view:
>
>   def GenerateReportDate(request, vehicle_id, date):
>     # removed verification code for clarity
>     # week_start = monday before date.
>     # week_end  = week_start + datetime.timedelta(7)
>     for i in range(7):
>       check_date = week_start + datetime.timedelta(i)
>       obj, created = StatusReport.objects.get_or_create(vehicle = int
> (vehicle_id),
>                                                       date = check_date,
>                                                       defaults={'date': 
> check_date,
>                                                                 
> 'vehicle':vehicle[0]})
>    # Pull 7 reports for this week
>    reports = StatusReport.objects.filter(
>        vehicle__exact=vehicle_id
>        ).filter(
>        date__gte = week_start
>        ).filter(
>        date__lte = week_end
>        ).order_by("date")
>
>   ReportFormSet = modelformset_factory(StatusReport, extra=0)
>
>   if request.method == 'POST':
>     formset = ReportFormSet(request.POST, request.FILES)
>     if formset.is_valid():
>         formset.save()
>         return HttpResponseRedirect('../done/') # Redirect after POST
>   else:
>      formset = ReportFormSet(queryset=reports)
>   return render_to_response('ops/report.html', {'formset': formset,
>                                                 'number': vehicle_id,
>                                                 'weekof': week_start})
>
> Thanks for any help,
>
> -Josh
>
> [1]http://code.djangoproject.com/ticket/9039
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@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-users?hl=en
-~--~~~~--~~--~--~---



modelformset_factory error: (Hidden field id) with this None already exists.

2008-12-02 Thread cyberjack

Hi all, I've been stumped by this error for the past few days and
haven't made any progress. Could someone please help out?

  I've created a formset of seven entries, one for each day of the
previous week. The first submission works fine; all seven entries are
successfully submitted. Subsequent submissions, however, generate the
following error for each of the seven entries:

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

I found an django issue from a few months ago which might be related
[1]. However, this bug was resolved back in Oct. and I'm running
1.0.2, so I assume the CL is included. Is there any way to be
certain?

Here are my models, simplified for space:
  class Vehicle(models.Model):
number  = models.IntegerField(unique=True)

  class StatusReport(models.Model):
date  = models.DateField()
vehicle   = models.ForeignKey(Vehicle, to_field='number')
status= models.CharField
('status',max_length=8,choices=STATUS_REPORT_CHOICES)

class Meta:
  unique_together = ("date", "vehicle")

And here's my view:

  def GenerateReportDate(request, vehicle_id, date):
# removed verification code for clarity
# week_start = monday before date.
# week_end  = week_start + datetime.timedelta(7)
for i in range(7):
  check_date = week_start + datetime.timedelta(i)
  obj, created = StatusReport.objects.get_or_create(vehicle = int
(vehicle_id),
  date = check_date,
  defaults={'date': 
check_date,

'vehicle':vehicle[0]})
   # Pull 7 reports for this week
   reports = StatusReport.objects.filter(
   vehicle__exact=vehicle_id
   ).filter(
   date__gte = week_start
   ).filter(
   date__lte = week_end
   ).order_by("date")

  ReportFormSet = modelformset_factory(StatusReport, extra=0)

  if request.method == 'POST':
formset = ReportFormSet(request.POST, request.FILES)
if formset.is_valid():
formset.save()
return HttpResponseRedirect('../done/') # Redirect after POST
  else:
 formset = ReportFormSet(queryset=reports)
  return render_to_response('ops/report.html', {'formset': formset,
'number': vehicle_id,
'weekof': week_start})

Thanks for any help,

-Josh

[1] http://code.djangoproject.com/ticket/9039
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@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-users?hl=en
-~--~~~~--~~--~--~---



Re: How to return Admin to previous filters after save?

2008-12-02 Thread cyberjack

Thanks, that's good to know.

-Josh



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@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-users?hl=en
-~--~~~~--~~--~--~---



How to return Admin to previous filters after save?

2008-12-01 Thread cyberjack

I have an admin interface managing a few thousand records. Finding a
record requires heavy use of multiple list_filter selections. However,
once a user edits a record, the admin interface redirects to the root
admin URL and loses the filter state.

Is there an easy way to have the admin editor return to the calling
URL after a form submit?

 Ie, I'd like the Admin tool to redirect to
   /admin/app/?is_active__exact=0&type__exact=A&model__exact=B
instead of
  /admin/app/?

As a work-around, I have my users bookmark common filter
configurations, but it's still pretty frustrating. I feel like there
must be some way to customize the Admin save function, but I don't
know where to start.

Thanks,

-Josh
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@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-users?hl=en
-~--~~~~--~~--~--~---



Re: How to return Admin to previous filters after save?

2008-02-14 Thread cyberjack

> Not a solution but a workaround. I had exactly the same problem as a user, I 
> solved it by bookmarking the right url.

 I've recommended this to our users, but it's a real pain. Each user
is pretty active, so there is a lot of (load-bookmark, make edit, load-
bookmark, repeat) annoyance.

 Does anyone know how to have the admin page redirect to
HTTP_REFERER?

Thanks,

-Josh


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@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-users?hl=en
-~--~~~~--~~--~--~---



How to return Admin to previous filters after save?

2008-02-13 Thread cyberjack

I have an admin interface managing a few thousand records. Finding a
record requires heavy use of multiple list_filter selections. However,
once a user edits a record, the admin interface redirects to the root
admin URL and loses the filter state.

Is there an easy way to have the admin editor return to the calling
URL after a form submit?

 Ie, I'd like the Admin tool to redirect to
   /admin/foo/?is_active__exact=0&type__exact=A&model__exact=B
instead of
  /admin/foo/?

Thanks,

-Josh
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@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-users?hl=en
-~--~~~~--~~--~--~---