Django:在M2M内联记录中包含一个主键字段

2024-04-26 10:48:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个产品模型,其中包含对期权模型的M2M引用,使用中间模型OptionPrice,它允许我用产品特定的值覆盖期权的基本值。你知道吗

以下是三种模型:

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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 to 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) 

,以及管理员.py,我将产品选项定义为内联

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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
    extra = 0
    save_on_top = True

我的问题是,当我在administrator中加载一个产品时,内联线显示得非常漂亮,除了主键(optionpriceid)之外,其他所有字段都显示出来。因此,我第一次向这个产品添加选项时,它们会正确保存(自动创建新的id)。如果然后尝试重新加载产品并编辑保存的选项,则会得到一个多值dictkeyerror,因为这些内联项实际上没有包含在管理员发出的行中的主键值。你知道吗

我做错什么了?你知道吗


Tags: name模型truedefaultverbosedb产品models
1条回答
网友
1楼 · 发布于 2024-04-26 10:48:57

这个问题原来是一个django bug,由this pull request解决。问题是,我使用了一个自定义UUIDField作为主键-引用票据:

It seems to be that this this problem happens for models which have a primary key field that is not based on the AutoField (such as a CharField).

When the primary key field is not based on an AutoField 'has_auto_field' is not set to True, therefore it will not output the primary key in the form.

相关问题 更多 >