OneToOnes + primary_key = True ... fails in Admin
|Reported by:||magneto||Owned by:||brosner|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||yes||Patch needs improvement:||yes|
This goes back to the primary keys and missing in the Admin forms as hidden fields, but it also effect 2 things . I've brought this up before #7938 for instance, where a slightly different bug was mended.
1) Addition of an element
2) editing a OneToOne directly
#create a simple model from django.db import models class BaseMoo(models.Model): name = models.CharField(max_length = 50) is_moo = models.BooleanField(default = True) class MooOne(models.Model): basemoo = models.OneToOneField(BaseMoo, primary_key = True) is_moo_one = models.BooleanField(default = True)
#create the ADmin from testmod.models import BaseMoo, MooOne from django.contrib import admin class MooOne_Inline(admin.StackedInline): model = MooOne extra = 1 max_num = 1 raw_id_fields = ('basemoo',) class BaseMooOptions(admin.ModelAdmin): inlines = [MooOne_Inline] list_display = ('name', 'is_moo',) class MooOneOptions(admin.ModelAdmin): raw_id_fields = ('basemoo',) list_display = ('basemoo', 'is_moo_one',) admin.site.register(BaseMoo, BaseMooOptions) admin.site.register(MooOne, MooOneOptions)
1) In Admin, try to 'add' a BaseMoo Object. It will give you the option for the Inlined MooOne, but on save the MooOne object is _not_ saved
2) ok a work around (but assumes that MooOne is registered as not an inline like above), so try to add the MooOne directly buy adding one attached to BaseMoo ..
3) Go back to the BaseMoo object, try to edit and save .. boom
Traceback: File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/django/core/handlers/base.py" in get_response 86. response = callback(request, *callback_args, **callback_kwargs) File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/django/contrib/admin/sites.py" in root 173. return self.model_page(request, *url.split('/', 2)) File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/django/views/decorators/cache.py" in _wrapped_view_func 44. response = view_func(request, *args, **kwargs) File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/django/contrib/admin/sites.py" in model_page 192. return admin_obj(request, rest_of_url) File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/django/contrib/admin/options.py" in __call__ 191. return self.change_view(request, unquote(url)) File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/django/db/transaction.py" in _commit_on_success 238. res = func(*args, **kw) File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/django/contrib/admin/options.py" in change_view 573. self.save_formset(request, form, formset, change=True) File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/django/contrib/admin/options.py" in save_formset 373. formset.save() File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/django/forms/models.py" in save 280. return self.save_existing_objects(commit) + self.save_new_objects(commit) File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/django/forms/models.py" in save_existing_objects 294. obj = existing_objects[form.cleaned_data[self.model._meta.pk.attname]] Exception Type: KeyError at /testmod/basemoo/34/ Exception Value: 'basemoo_id'
4) Ok so now go back to the MooOne raw object and try to edit it. Well you can, except now the raw_id_fields of 'BaseMoo" is NOT filled in automatically, (If i turn off raw_id_fields, the select box does _not_ choose the current BaseMoo Object). Meaning the 'save' will fail until you pick the old object again.
I Imagine all of these little issues are related to the primary_key vs autofield display bits in admin.
Change History (23)
comment:1 Changed 6 years ago by magneto
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
- Summary changed from OneToOnes + Inlines + raw_id_fields fails in Admin to OneToOnes + Inlines fails in Admin
comment:2 Changed 6 years ago by magneto
- Summary changed from OneToOnes + Inlines fails in Admin to OneToOnes + primary_key = True ... fails in Admin
comment:7 follow-up: ↓ 8 Changed 6 years ago by kmtracey
- Triage Stage changed from Unreviewed to Accepted
comment:12 Changed 6 years ago by brosner
- Owner changed from nobody to brosner
- Status changed from new to assigned
comment:19 Changed 6 years ago by brosner
- Resolution set to duplicate
- Status changed from assigned to closed