{"id": 8562, "created": "2008-08-26 18:09:13", "changetime": "2011-09-28 16:12:17", "last_pulled_from_trac": "2022-03-06 03:42:35.101521", "stage": "Accepted", "status": "closed", "component": "contrib.admin", "type": null, "severity": null, "version": "dev", "resolution": "duplicate", "summary": "OneToOnes + primary_key = True ... fails in Admin", "description": "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.\r\n\r\n1) Addition of an element\r\n2) editing a OneToOne directly\r\n\r\n{{{\r\n#create a simple model\r\nfrom django.db import models\r\n\r\nclass BaseMoo(models.Model):\r\n\tname = models.CharField(max_length = 50)\r\n\tis_moo = models.BooleanField(default = True)\r\n\r\n\r\nclass MooOne(models.Model):\r\n\tbasemoo = models.OneToOneField(BaseMoo, primary_key = True)\r\n\tis_moo_one = models.BooleanField(default = True)\r\n\r\n}}}\r\n\r\n{{{ \r\n#create the ADmin\r\nfrom testmod.models import BaseMoo, MooOne\r\n\r\nfrom django.contrib import admin\r\n\r\nclass MooOne_Inline(admin.StackedInline):\r\n\tmodel = MooOne\r\n\textra = 1\r\n\tmax_num = 1\r\n\traw_id_fields = ('basemoo',)\r\n\t\r\nclass BaseMooOptions(admin.ModelAdmin):\r\n\tinlines = [MooOne_Inline]\r\n\tlist_display = ('name', 'is_moo',)\r\n\r\nclass MooOneOptions(admin.ModelAdmin):\r\n\traw_id_fields = ('basemoo',)\r\n\tlist_display = ('basemoo', 'is_moo_one',)\r\n\r\nadmin.site.register(BaseMoo, BaseMooOptions)\r\nadmin.site.register(MooOne, MooOneOptions)\r\n\r\n}}}\r\n\r\n1) 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\r\n\r\n2) 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 .. \r\n\r\n3) Go back to the BaseMoo object, try to edit and save .. boom\r\n\r\n{{{\r\nTraceback:\r\nFile \"/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/django/core/handlers/base.py\" in get_response\r\n 86. response = callback(request, *callback_args, **callback_kwargs)\r\nFile \"/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/django/contrib/admin/sites.py\" in root\r\n 173. return self.model_page(request, *url.split('/', 2))\r\nFile \"/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/django/views/decorators/cache.py\" in _wrapped_view_func\r\n 44. response = view_func(request, *args, **kwargs)\r\nFile \"/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/django/contrib/admin/sites.py\" in model_page\r\n 192. return admin_obj(request, rest_of_url)\r\nFile \"/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/django/contrib/admin/options.py\" in __call__\r\n 191. return self.change_view(request, unquote(url))\r\nFile \"/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/django/db/transaction.py\" in _commit_on_success\r\n 238. res = func(*args, **kw)\r\nFile \"/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/django/contrib/admin/options.py\" in change_view\r\n 573. self.save_formset(request, form, formset, change=True)\r\nFile \"/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/django/contrib/admin/options.py\" in save_formset\r\n 373. formset.save()\r\nFile \"/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/django/forms/models.py\" in save\r\n 280. return self.save_existing_objects(commit) + self.save_new_objects(commit)\r\nFile \"/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/django/forms/models.py\" in save_existing_objects\r\n 294. obj = existing_objects[form.cleaned_data[self.model._meta.pk.attname]]\r\n\r\nException Type: KeyError at /testmod/basemoo/34/\r\nException Value: 'basemoo_id'\r\n}}}\r\n\r\n4) 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.\r\n\r\nI Imagine all of these little issues are related to the primary_key vs autofield display bits in admin.\r\n\r\n", "owner": "brosner", "reporter": "magneto", "keywords": "admin onetoone", "easy": 0, "has_patch": 1, "needs_better_patch": 1, "needs_tests": 1, "needs_docs": 0, "ui_ux": 0}