mirumee / django-prices

Django fields for the prices module
158 stars 53 forks source link

PriceField with default, and inline models in admin #12

Closed brew closed 11 years ago

brew commented 11 years ago

I have Parent and Child models in myapp/models.py. The MyChild model has a PriceField field:

from django.db import models
from django_prices.models import PriceField

class MyParent(models.Model):
    name = models.CharField(max_length=30)

class MyChild(models.Model):
    name = models.CharField(max_length=30)

    # note default attribute is present
    price_offset = PriceField("unit price offset", currency='USD', max_digits=12, decimal_places=4, default=0)

    parent = models.ForeignKey(MyParent, related_name='children')

And in myapp/admin.py, the child models are inline:

from django.contrib import admin
import models

class ChildInline(admin.TabularInline):
    model = models.MyChild
    extra = 3

class ParentAdmin(admin.ModelAdmin):
    inlines = (ChildInline,)
admin.site.register(models.MyParent, ParentAdmin)

If I attempt to add a Parent using the admin, by filling the Parent's name field, but leaving the three child forms untouched, I get errors because the three empty children are also being submitted.

inline_pricefield_problem

(In fact, if MyChild.name was able to validate, either having its own default value, or blank=True parameter, the form would submit successfully and three MyChild instances would be saved to the database. Three new MyChild instances would be created each time the Parent instance is saved using the admin.)

This is triggered by the presence of the default parameter of the MyChild.price_offset field. If I remove the default, I can submit without an attempt to create child instances.

patrys commented 11 years ago

I will try to investigate it in the coming days but it would be really good if you could provide at least a unit test to make things easier.

brew commented 11 years ago

As it's quite specific to the interaction with the admin interface, I'm not sure how best to write a unittest for it. I can zip up the toy project I made for it, if that would help?

http://cl.ly/QIpX

patrys commented 11 years ago

Fixed in 7cb34f8b0986f3330ba1af397a1795e9e2fc2820 and released as 0.2.3