python - Django: include a primary key field in an M2M inline record -
i have product model includes m2m reference option model, using intermediate model, optionprice, lets me override base values of option product-specific values.
here 3 models:
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ class product(models.model): productid = uuidfield(db_column='productid', editable=false, primary_key=true, auto=true, hyphenate=true) productname = models.charfield(db_column='productname', max_length=100, verbose_name='name', default='') slug = autoslugfield(populate_from='productname',unique=true, max_length=100, always_update=true) productnumber = models.charfield(db_column='productnumber', blank=true, max_length=50, null=true, verbose_name='number', default='') cost = models.decimalfield(decimal_places=4, max_digits=19, db_column='cost', default=0) priceperitem = models.decimalfield(decimal_places=4, max_digits=19, db_column='priceperitem', verbose_name='price per item', default=0) onspecial = models.booleanfield(db_column='onspecial', verbose_name='on special', default=false) discount = models.floatfield(null=true, db_column='discount', blank=true, verbose_name='discounted', default=0) #connections other models options = models.manytomanyfield(option, null=true, through='optionprice', verbose_name='product options', blank=true) optiongroups = models.manytomanyfield(optiongroup, null=true, verbose_name='product option groups', blank=true) package = models.foreignkey(shippingpackage, verbose_name='shipping container', default=1) categories = models.manytomanyfield(category, verbose_name='product categories') cross_sell = models.manytomanyfield('product', verbose_name='cross-sell items', blank=true) #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ class option(models.model): optionid = uuidfield(db_column='optionid', editable=false, primary_key=true, auto=true, hyphenate=true) title = models.charfield(db_column='title', max_length=500) price = models.decimalfield(decimal_places=4, max_digits=19, db_column='price') pricemodstyle = models.charfield(db_column='pricemodstyle', max_length=50, verbose_name='price modification style', choices=price_mod_choices) displayrank = models.integerfield(db_column='displayrank', verbose_name='sort order') optiongroup = models.foreignkey('optiongroup', verbose_name='option group') #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ class optionprice(models.model): optionpriceid = uuidfield(db_column='optionpriceid', editable=false, primary_key=true, auto=true, hyphenate=true) option = models.foreignkey(option) product = models.foreignkey(product) price = models.decimalfield(decimal_places=4, max_digits=19, db_column='price') pricemodstyle = models.charfield(db_column='pricemodstyle', max_length=50, verbose_name='price modification style', choices=price_mod_choices) , , admin.py, have product options defined being inline
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ class productadmin(importexportmodeladmin): resource_class = productresource model=product ordering = ['categories__categoryname','productname'] save_on_top = true save_as = true inlines = [optionpriceadmininline] #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ class optionpriceadmininline(admin.tabularinline): model=optionprice = 0 save_on_top = true my issue when load product in administrator, inlines show beautifully, with every field represented except primary key (optionpriceid). consequently, first time add options product, save (new id created automagically). if attempt reload product , edit saved options, multivaluedictkeyerror because these inlined items don't have primary key values included in rows emitted admin.
what doing wrong?
the issue turned out django bug, solved this pull request. problem using custom uuidfield primary key - quote ticket:
it seems this problem happens models have primary key field not based on autofield (such charfield).
when primary key field not based on autofield 'has_auto_field' not set true, therefore not output primary key in form.
Comments
Post a Comment