Opened 2 months ago

Last modified 2 months ago

#29237 new Bug

ModelAdmin.inlines cause a model to lose its primary key from a model field default

Reported by: Gunnar Thielebein Owned by: nobody
Component: contrib.admin Version: 1.11
Severity: Normal Keywords:
Cc: 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 (2)

comment:1 Changed 2 months ago 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.

comment:2 Changed 2 months ago by Tim Graham

Summary: Integrity Error in ModelAdmin with inlines and Foreign Key Reference populated by models defaultModelAdmin.inlines cause a model to lose its primary key from a model field default
Triage Stage: UnreviewedAccepted

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.

Note: See TracTickets for help on using tickets.
Back to Top