Tuesday, June 23, 2015

Re: ModelAdmin.save_as integrity error

On 24/06/2015 3:24 AM, Luis Zárate wrote:
> Can I see your Substance save method? Maybe there are something wrong.

Luis

Thanks for responding. There is/was something wrong. I was creating
related objects in the save() method which is fine for a new substance
but when [Save as new] happened the Admin didn't know that and
replicated existing related objects. It was getting somewhat out of hand.

My solution FWIW is to use try/except around my own related object
creation and pass if already there. For the moment that seems to be working.

Cheers

Mike


>
>
>
> El martes, 23 de junio de 2015, Mike Dewhirst <miked@dewhirst.com.au
> <mailto:miked@dewhirst.com.au>> escribió:
> > I commented out the save() method in the Substance model (the master
> model with a number of 1:1 and 1:n relationships) and it started saving
> as new successfully. Unfortunately I do a lot of stuff in that save()
> method so I have to figure out how to do that another way.
> >
> > Mike
> >
> > On 23/06/2015 12:57 PM, Mike Dewhirst wrote:
> >>
> >> The following is all I could find in the docs about ModelAdmin.save_as
> >>
> >> ModelAdmin.save_as
> >>
> (https://docs.djangoproject.com/en/1.7/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_as)
> >>
> >>
> >>    Set save_as to enable a â€Å"save as†feature on
> admin change forms.
> >>
> >>    Normally, objects have three save options: â€Å"Saveâ€
> , â€Å"Save and
> >> continue editing†and â€Å"Save and add another†. If
> save_as is True,
> >> â€Å"Save and add another†will be replaced by a â€Å"Save
> as†button.
> >>
> >>    â€Å"Save as†means the object will be saved as a
> new object (with
> >> a new ID), rather than the old object.
> >>
> >> Â Â Â By default, save_as is set to False.
> >>
> >>
> >>
> >> I'm having problems with [save as new] when the model has inlines. Is
> >> there a known gotcha in this area?
> >>
> >> The particular error I'm seeing is ...
> >>
> >>
> >>
> >> IntegrityError at /admin/substance/substance/20/
> >>
> >> duplicate key value violates unique constraint
> >> "substance_liquid_substance_id_key"
> >> DETAIL:Â Key (substance_id)=(54) already exists.
> >>
> >> Request Method:Â Â Â POST
> >> Request URL:Â Â Â http://localhost:8000/admin/substance/substance/20/
> >> Django Version:Â Â Â 1.7.8
> >> Exception Type:Â Â Â IntegrityError
> >> Exception Value:
> >>
> >> duplicate key value violates unique constraint
> >> "substance_liquid_substance_id_key"
> >> DETAIL:Â Key (substance_id)=(54) already exists.
> >>
> >> Exception Location:
> >> C:\Users\mike\env\xxex3\lib\site-packages\django\db\backends\utils.py in
> >> execute, line 65
> >> Python Executable:Â Â Â C:\Users\mike\env\xxex3\Scripts\python.exe
> >> Python Version:Â Â Â 3.4.3
> >> Python Path:
> >>
> >> ['/var/www/ssds',
> >> Â 'C:\\Users\\mike\\env\\xxex3\\ssds',
> >>
> >>
> 'C:\\Users\\mike\\env\\xxex3\\lib\\site-packages\\psycopg2-2.5.4-py3.4-win32.egg',
> >>
> >>
> >>
> 'C:\\Users\\mike\\env\\xxex3\\lib\\site-packages\\pillow-2.7.0-py3.4-win32.egg',
> >>
> >> Â 'C:\\WINDOWS\\SYSTEM32\\python34.zip',
> >> Â 'C:\\Users\\mike\\env\\xxex3\\DLLs',
> >> Â 'C:\\Users\\mike\\env\\xxex3\\lib',
> >> Â 'C:\\Users\\mike\\env\\xxex3\\Scripts',
> >> Â 'C:\\Python34\\Lib',
> >> Â 'C:\\Python34\\DLLs',
> >> Â 'C:\\Users\\mike\\env\\xxex3',
> >> Â 'C:\\Users\\mike\\env\\xxex3\\lib\\site-packages',
> >> Â 'C:/Users/mike/env/xxex3/ssds']
> >>
> >> Server time:Â Â Â Tue, 23 Jun 2015 12:08:44 +1000
> >>
> >>
> >> The relationship between substance and liquid is 1:1 and the error is
> >> highlighted in my liquid.save() method. However, when I comment that
> >> whole save() method out and try [save as new] again the error doesn't
> >> change, just the substance pk which increments from 54 (as above) to 55
> >> and none of my code seems to be involved. Or at least the traceback
> >> doesn't highlight any.
> >>
> >> There appear to be no problems with the code saving normally and there
> >> are hundreds of unit tests all passing successfully.
> >>
> >> I would really appreciate any help. Is there more info I can provide?
> >>
> >> Python 3.4 (Windows 8.1)
> >> Django 1.7.8 (Windows 8.1)
> >> Postgres 9.1 (Ubuntu 12.04)
> >>
> >> Thanks for any hints.
> >>
> >> Mike
> >>
> >
> > --
> > You received this message because you are subscribed to the Google
> Groups "Django users" group.
> > To unsubscribe from this group and stop receiving emails from it,
> send an email to django-users+unsubscribe@googlegroups.com
> <mailto:django-users%2Bunsubscribe@googlegroups.com>.
> > To post to this group, send email to django-users@googlegroups.com
> <mailto:django-users@googlegroups.com>.
> > Visit this group at http://groups.google.com/group/django-users.
> > To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/5588F7B9.1030906%40dewhirst.com.au.
> > For more options, visit https://groups.google.com/d/optout.
> >
>
> --
> "La utopía sirve para caminar" Fernando Birri
>
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users+unsubscribe@googlegroups.com
> <mailto:django-users+unsubscribe@googlegroups.com>.
> To post to this group, send email to django-users@googlegroups.com
> <mailto:django-users@googlegroups.com>.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAG%2B5VyPKTdDLEMdU5RoeA-Q2CFK42DhPQ5ooqGVCKrHKq447Rw%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAG%2B5VyPKTdDLEMdU5RoeA-Q2CFK42DhPQ5ooqGVCKrHKq447Rw%40mail.gmail.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5589C362.2050606%40dewhirst.com.au.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment