在管理面板中显示其他模型的数据

时间:2012-10-03 10:17:17

标签: django django-admin

如何在OrderAdminBasket所有者,产品,产品数量中显示?

我尝试内联:

admin.py:

class BasketInline(admin.TabularInline):
    model = Basket

class OrderAdmin(admin.ModelAdmin):
    inlines = [
        BasketInline,
    ]
admin.site.register(Order, OrderAdmin)

但它不起作用。

class Basket(models.Model):
    owner = models.ForeignKey(User, related_name='user_basket', verbose_name='Owner')
    name = models.CharField("Basket_Name", max_length=30)
    products = models.ManyToManyField('Product', through='BasketProduct', blank=True, null=True)


class BasketProduct(models.Model):
    product = models.ForeignKey('Product')
    basket = models.ForeignKey('Basket')
    quantity = models.IntegerField()

class Product(models.Model):
    name = models.CharField(max_length=50)
    slug = models.SlugField()
    unit_price = models.DecimalField(max_digits=5, decimal_places=2)
    desc = models.TextField()
    category = models.ManyToManyField(Category)

class ShippingOptions(models.Model):
    name = models.CharField(max_length=50)
    price = models.DecimalField(max_digits=5, decimal_places=2)
    time = models.CharField(max_length=150)

class Order(models.Model):
    bask = models.OneToOneField(Basket)
    shipp = models.OneToOneField(ShippingOptions)

也许我需要一种不同的方式。请帮忙。感谢

1 个答案:

答案 0 :(得分:0)

Order模型中的Basket需要foreignKey:

class Basket(models.Model):
    owner = models.ForeignKey(User, related_name='user_basket', verbose_name='Owner')
    name = models.CharField("Basket_Name", max_length=30)
    products = models.ManyToManyField('Product', through='BasketProduct', blank=True, null=True)
    owner = models.ForeignKey(Owner, blank=True, verbose_name='Owner')

或者您在第一个模型行中有错误:

owner = models.ForeignKey(Owner, related_name='user_basket', verbose_name='Owner')