首页 > 解决方案 > 表格内联管理员,其中表在多个表中引用

问题描述

我正在寻找自定义管理员,在那里我找到tabularinline了允许我们在同一页面上进行编辑的模型。但是我很困惑,因为我的表格在多个地方都有参考。我不明白我应该将哪个模型制作成表格内联。

例如这是我的模型

class ProductType(ModelWithMetadata):
    name = models.CharField(max_length=150, help_text="type of product. ex - accessories, apparel")
    slug = models.SlugField(max_length=150, unique=True)
    is_shipping_required = models.BooleanField(default=True)

    class Meta:
        ordering = ("slug",)
        app_label="products"

class Category(MPTTModel, ModelWithMetadata):
    name = models.CharField(max_length=128)
    slug = models.SlugField(max_length=128)
    description = models.TextField(blank=True)
    parent = models.ForeignKey(
        "self", null=True, blank=True, related_name="children", on_delete=models.CASCADE
    )
    
    objects = models.Manager()
    tree = TreeManager()

    class Meta:
        verbose_name = "Category"
        verbose_name_plural = "Categories"

    def __str__(self):
        return self.name


class Product(ModelWithMetadata, PublishableModel):
    product_type = models.ForeignKey(
        ProductType, related_name="products", on_delete=models.CASCADE
    )
    name = models.CharField(max_length=128)
    slug = models.SlugField()
    category = models.ForeignKey(
        Category,
        related_name="products",
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
    )
    isAvailable = models.BooleanField(default=True)
    isDiscount = models.BooleanField(default=False)
    charge_taxes = models.BooleanField(default=False)
    updated_at = models.DateTimeField(auto_now=True, null=True)

    class Meta:
        verbose_name = "product"
        verbose_name_plural = "products"
        ordering = ("name",)
        constraints = [
            models.UniqueConstraint(
                fields=["article_number", "slug"], name="unique article number and slug")
        ]

class ProductVariant(ModelWithMetadata):
    sku = models.CharField(max_length=255, unique=True)
    name = models.CharField(max_length=255, blank=True)
    currency = models.CharField(
        max_length=settings.DEFAULT_CURRENCY_CODE_LENGTH,
        default=settings.DEFAULT_CURRENCY,
        blank=True,
        null=True,
    )
    price_override_amount = models.DecimalField(
        max_digits=settings.DEFAULT_MAX_DIGITS,
        decimal_places=settings.DEFAULT_DECIMAL_PLACES,
        blank=True,
        null=True,
    )
    product = models.ForeignKey(
        Product, related_name="variants", on_delete=models.CASCADE
    )
    images = models.ManyToManyField("ProductImage", through="VariantImage")
    track_inventory = models.BooleanField(default=True)


class BaseAssignedAttribute(models.Model):

    assignment = None
    values = models.ManyToManyField("AttributeValue")


class AttributeValue(models.Model):

    name = models.CharField(max_length=250)
    value = models.CharField(max_length=100, blank=True, default="")
    slug = models.SlugField(max_length=255)
    attribute = models.ForeignKey(
        "Attribute", related_name="values", on_delete=models.CASCADE
    )


class AssignedProductAttribute(BaseAssignedAttribute):
    """Associate a product type attribute and selected values to a given product."""

    product = models.ForeignKey(
        Product, related_name="attributes", on_delete=models.CASCADE
    )
    assignment = models.ForeignKey(
        "ProductAttribute", on_delete=models.CASCADE, related_name="productassignments"
    )

    class Meta:
        unique_together = (("product", "assignment"),)


class AssignedVariantAttribute(BaseAssignedAttribute):
    """Associate a product type attribute and selected values to a given variant."""

    variant = models.ForeignKey(
        ProductVariant, related_name="attributes", on_delete=models.CASCADE
    )
    assignment = models.ForeignKey(
        "AttributeVariant", on_delete=models.CASCADE, related_name="variantassignments"
    )

    class Meta:
        unique_together = (("variant", "assignment"),)


class AttributeVariant(models.Model):
    attribute = models.ForeignKey(
        "Attribute", related_name="attributevariant", on_delete=models.CASCADE
    )
    product_type = models.ForeignKey(
        ProductType, related_name="attributevariant", on_delete=models.CASCADE
    )
    assigned_variants = models.ManyToManyField(
        ProductVariant,
        blank=True,
        through=AssignedVariantAttribute,
        through_fields=("assignment", "variant"),
        related_name="attributesrelated",
    )


class Attribute(models.Model):
    name = models.CharField(max_length=30, unique=True)
    slug = models.SlugField(max_length=250, unique=True)
    product_types = models.ManyToManyField(
        ProductType,
        blank=True,
        related_name="product_attributes",
        through="ProductAttribute",
        through_fields=("attribute", "product_type"),
    )
    product_variant_types = models.ManyToManyField(
        ProductType,
        blank=True,
        related_name="variant_attributes",
        through=AttributeVariant,
        through_fields=("attribute", "product_type"),
    )


class ProductAttribute(models.Model):
    attribute = models.ForeignKey(
        "Attribute", related_name="attributeproduct", on_delete=models.CASCADE
    )
    product_type = models.ForeignKey(
        ProductType, related_name="attributeproduct", on_delete=models.CASCADE
    )
    assigned_products = models.ManyToManyField(
        Product,
        blank=True,
        through=AssignedProductAttribute,
        through_fields=("assignment", "product"),
        related_name="attributesrelated",
    )

我对桌子AttributeValueAssignedProductAttributeAssignedVariantAttributeAttributeVariantAttribute感到困惑ProductAttribute。Attribute 与 ProductAttribute 相关,也与 AttributeVariant 和 AttributeValue 相关。同样,在变体的情况下。

我不知道哪个表应该是内联的,我应该在哪里引用该内联表。由于各种关系,我不确定这些事情。

标签: pythondjangodjango-modelsdjango-admin

解决方案


推荐阅读