Opened 8 years ago
Last modified 7 years ago
#29237 assigned Bug
ModelAdmin.inlines cause a model to lose its primary key from a model field default
| Reported by: | Gunnar Thielebein | Owned by: | Sasha Gaevsky |
|---|---|---|---|
| Component: | contrib.admin | Version: | 1.11 |
| Severity: | Normal | Keywords: | |
| Cc: | Jeff, Andrey Maslov | Triage Stage: | Accepted |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
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
Change History (5)
comment:1 by , 8 years ago
comment:2 by , 8 years ago
| Summary: | Integrity Error in ModelAdmin with inlines and Foreign Key Reference populated by models default → ModelAdmin.inlines cause a model to lose its primary key from a model field default |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
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.
comment:3 by , 7 years ago
| Cc: | added |
|---|
comment:4 by , 7 years ago
| Cc: | added |
|---|
comment:5 by , 7 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
Workaround for me is to remove the
to_fieldin 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.