0

I have a model PlacedOrder that is referenced by other models using OneToOneField and ForeignKey. So I am using StackedTabularInline to render it on my PlacedOrderAdmin model.

The weird behavior is that when I start my django application I can create a new PlacedOrder object with no problems but after that when I try to create another object the fields on the inlines are already filled with the content from the object that I just created and I can't create a new object no matter what I try, it keeps showing me the error "Please correct the errors below."

Only the fields that belongs to the inlines does that, the fields from the model PlacedOrder are "clean". If I restart django I can see all the objects created and their data seems correct.

part of the Models:

class PlacedOrder(models.Model):
    id = models.UUIDField(
        primary_key=True,
        default=uuid.uuid4(),
        editable=False
    )
    ...
    total = models.DecimalField(
        _('total'),
        max_digits=8,
        decimal_places=2
    )


class OrderStatus(models.Model):
    placed_order = models.OneToOneField(
        PlacedOrder,
        on_delete=models.CASCADE
    )
    status = models.CharField(
        _('status'),
        choices=STATUS_CHOICES,
        max_length=30,
    )

Here is one of the inlines:

class OrderStatusInline(nested_admin.NestedTabularInline):
    model = app_models.OrderStatus

and the Order

@admin.register(app_models.PlacedOrder)
class OrderAdmin(nested_admin.NestedModelAdmin):
    inlines = (OrderStatusInline, OrderPaymentInline, OrderDeliveryInline, SelectedProductInline, )

Fixed it

So I found out that the problem had nothing to do with what I posted here, it was actually how I was declaring the id inside PlacedOrder.

On my original model I had the default set as uuid.uuid4() but that was creating all the problem, once I changed it to uuid.uuid4 everything was fine.

1 Answers1

0

I think the problem is that with a OneToOne relationship the actual field defining the relationship has to be on the inline model, not the parent one - in just the same way as for a ForeignKey

Take a look at this post for more info: Django Admin: OneToOne Relation as an Inline?

HenryM
  • 5,557
  • 7
  • 49
  • 105
  • But that is how it is right now. The `PlacedOrder` is the parent one and `OrderStatus` which has the `OneToOne` is referenced inside the `OrderStatusInline`. I will double check but I'm almost sure that all the inlines are already like that. – Thales Menato Apr 04 '19 at 14:53