{{{
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.
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>
* 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>
* cc: Jeff (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/29237#comment:3>
* cc: Andrey Maslov (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/29237#comment:4>
* status: new => assigned
* owner: nobody => Sasha Gaevsky
--
Ticket URL: <https://code.djangoproject.com/ticket/29237#comment:5>