[Django] #29237: Integrity Error in ModelAdmin with inlines and Foreign Key Reference populated by models default

20 views
Skip to first unread message

Django

unread,
Mar 18, 2018, 4:14:26 PM3/18/18
to django-...@googlegroups.com
#29237: Integrity Error in ModelAdmin with inlines and Foreign Key Reference
populated by models default
---------------------------------------------+------------------------
Reporter: Gunnar Thielebein | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 1.11
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
---------------------------------------------+------------------------
models.py

{{{
from django.db import models
from django.contrib.auth.models import User
import uuid

class Pizza(models.Model):
name = models.CharField(max_length=40, unique=True,
verbose_name='Name')

def __unicode__(self):
return self.name


class Topping(models.Model):
name = models.CharField(max_length=40, unique=True,
verbose_name='Name')

def __unicode__(self):
return self.name


class Size(models.Model):
size = models.CharField(max_length=40, unique=True,
verbose_name='Size')

def __unicode__(self):
return self.size


class Ordering(models.Model):
uuid = models.UUIDField(default=uuid.uuid4, unique=True,
editable=False, verbose_name="UUID")
customer = models.ForeignKey('Customer', to_field='uuid',
verbose_name='Customer')
pizza = models.ForeignKey(Pizza, to_field='name',
verbose_name='Pizza')
size = models.ForeignKey(Size, to_field='size', verbose_name='Size')
toppings = models.ManyToManyField(Topping, verbose_name='Toppings')

def __unicode__(self):
return str(self.uuid)

class Customer(User):
uuid = models.UUIDField(default=uuid.uuid4, unique=True,
editable=False, verbose_name="UUID")
pizzas = models.ManyToManyField(Pizza, through=Ordering, blank=True,
verbose_name='Pizzas Ordered')

class Meta:
verbose_name_plural = "Customers"
}}}

admin.py:

{{{
from django.contrib import admin
from .models import *

from django.forms import modelformset_factory, inlineformset_factory,
ModelForm

OrderingFormset = inlineformset_factory(Pizza, Ordering, fields=('pizza',
'toppings', 'size'))

class PizzaInline(admin.TabularInline):
model = Ordering
formset = OrderingFormset

class CustomerAdmin(admin.ModelAdmin):

inlines = [
PizzaInline,
]


admin.site.register(Customer, CustomerAdmin)
admin.site.register(Ordering)
admin.site.register(Pizza)
admin.site.register(Topping)
admin.site.register(Size)
}}}

There is an issue when adding a new customer with the CustomerModelAdmin
in backend.
When creating a the following error is raised:


IntegrityError at /admin/tabtest/customer/add/
NOT NULL constraint failed: tabtest_customer.uuid

--
Ticket URL: <https://code.djangoproject.com/ticket/29237>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 18, 2018, 4:29:08 PM3/18/18
to django-...@googlegroups.com
#29237: Integrity Error in ModelAdmin with inlines and Foreign Key Reference
populated by models default
-----------------------------------+--------------------------------------

Reporter: Gunnar Thielebein | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 1.11
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------

Comment (by Gunnar Thielebein):

Workaround for me is to remove the {{{to_field}}} in this line.

{{{customer = models.ForeignKey('Customer', to_field='uuid',
verbose_name='Customer')}}}

It seems AutoField (field id) is populated earlier than empty UUIDField
with default callable.
Whats strange is if I set customer.uuid.editable to True, the UUID is
already rendered on form but the same exception is thrown.

--
Ticket URL: <https://code.djangoproject.com/ticket/29237#comment:1>

Django

unread,
Mar 20, 2018, 8:40:55 PM3/20/18
to django-...@googlegroups.com
#29237: ModelAdmin.inlines cause a model to lose its primary key from a model field
default
-----------------------------------+------------------------------------

Reporter: Gunnar Thielebein | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 1.11
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+------------------------------------
Changes (by Tim Graham):

* stage: Unreviewed => Accepted


Comment:

I tracked the problem to `ModelAdmin._create_formsets()` causing
`new_object` to lose it's primary key. The call is `formsets,
inline_instances = self._create_formsets(request, new_object, change=not
add)` in `_changeform_view()`. Removing `inlines = [PizzaInline]` from
`CustomerAdmin` makes the issue go away. `formset = OrderingFormset` isn't
required to reproduce the issue.

--
Ticket URL: <https://code.djangoproject.com/ticket/29237#comment:2>

Django

unread,
Jun 29, 2018, 10:59:14 AM6/29/18
to django-...@googlegroups.com
#29237: ModelAdmin.inlines cause a model to lose its primary key from a model field
default
-----------------------------------+------------------------------------
Reporter: Gunnar Thielebein | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 1.11
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+------------------------------------
Changes (by Jeff):

* cc: Jeff (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/29237#comment:3>

Django

unread,
Dec 7, 2018, 4:10:22 AM12/7/18
to django-...@googlegroups.com
#29237: ModelAdmin.inlines cause a model to lose its primary key from a model field
default
-----------------------------------+------------------------------------
Reporter: Gunnar Thielebein | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 1.11
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+------------------------------------
Changes (by Andrey Maslov):

* cc: Andrey Maslov (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/29237#comment:4>

Django

unread,
Mar 3, 2019, 5:47:24 AM3/3/19
to django-...@googlegroups.com
#29237: ModelAdmin.inlines cause a model to lose its primary key from a model field
default
-------------------------------------+-------------------------------------
Reporter: Gunnar Thielebein | Owner: Sasha
| Gaevsky
Type: Bug | Status: assigned
Component: contrib.admin | Version: 1.11

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sasha Gaevsky):

* status: new => assigned
* owner: nobody => Sasha Gaevsky


--
Ticket URL: <https://code.djangoproject.com/ticket/29237#comment:5>

Reply all
Reply to author
Forward
0 new messages