[Answered ]-Django Admin One-To-Many-To-One (Site->Product->Price) Relationship

1👍

I figured this out about 20 minutes after posting. The key was to create a new model (named ItemPrice) with site, upc, and price — with site and upc FK’d to Site and Item. I then created a new Inline pointing to ItemPrice:


class ItemPrice(models.Model):
    site = models.ForeignKey(Site, on_delete=models.CASCADE, null=True)
    upc = models.ForeignKey(Item, on_delete=models.CASCADE, null=True, verbose_name="UPC")
    price = models.DecimalField(max_digits=6, decimal_places=2, default=0)

    class Meta:
        constraints = [
            models.UniqueConstraint(fields=['site', 'upc'], name='unique_site_upc')
        ]

and

class ItemPriceInline(admin.TabularInline):
    model = ItemPrice
    # can_delete = False
    verbose_name = 'Item Price'
    verbose_name_plural = 'Item Prices'
    extra = 0
👤p98376

Leave a comment