Django管理员模型关系

时间:2019-05-01 01:49:12

标签: django django-admin django-orm django-admin-filters django-admin-tools

因此,我正在构建一个Store网站,我有2个应用程序,一个名为Store的应用程序,它有两个模型“ Store”和“ UnitStore”。第二个应用程序称为“产品”,而其单个模型称为“商店”。

在产品管理员中,我使用formfield_for_foreignkey方法显示StoreUnit和商店,如下所示:“ Store_name | Unit_name”

所以,我的问题是,当我尝试创建产品时,它总是要求UnitStore的外键,但是并非所有产品都来自UnitStore,某些产品只能在Store中找到,因此,我不是能够创建实例。

- Store > Models.py 

class Store(models.Model):

    store_name = models.CharField(max_length=200)
    created_at = models.DateTimeField(auto_now_add=True)
        updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.store_name 

class UnitStore(models.Model):

    store_unit_name = models.CharField(max_length=200)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    store = models.ForeignKey(
        Store,
        related_name='units',
        on_delete=models.CASCADE,
    )

    def __str__(self):
        return self.store_unit_name 


Product > Models.py

class Product(models.Model):

    product= models.CharField(max_length=200)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    store_unit_name = models.ForeignKey(
        StoreUnit,
        related_name='units',
        on_delete=models.CASCADE,
    )

    def __str__(self):
        return self.product

- Product > Admin.py

class CustomProduct(admin.ModelAdmin):
    model = Product
    list_display = ('product', 'created_at', 'aupdated_at',
                    'store_unit_name',)
    list_select_related = ('store_unit_name')
    fieldsets = (
        ("Prodcut's Name:", {
            'fields': ('product',)}),
        ('Store:', {
            'fields': ('store_unit_name',)}),  
    )

    def save_model(self, request, obj, form, change):
        obj.created_by = request.user
        super().save_model(request, obj, form, change)

是否应该向Product模型添加另一个外键,如下面的示例所示。因此,我应该能够通过以下方式访问产品:product.store.store_name。

class Product(models.Model):

    product= models.CharField(max_length=200)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    store_unit_name = models.ForeignKey(
        StoreUnit,
        related_name='units',
        on_delete=models.CASCADE,
    )
  store= models.ForeignKey(
        Store,
        related_name='stores',
        on_delete=models.CASCADE,
    )

1 个答案:

答案 0 :(得分:0)

您需要具有一个同时捕获商店和单位商店的商店模型,并且其__str__方法应始终打印主商店名称和单位商店名称(如果适用):

class Store(models.Model):

    store_name = models.CharField(max_length=200)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    main_store = models.ForeignKey(
        'Store',
        null=True,
        related_name='units',
        on_delete=models.CASCADE,
    )

    def __str__(self):
        ret_value = self.main_store.store_name + " | " if self.main_store else ""
        return ret_value + self.store_name

一旦拥有了这样的模型,就可以只用一个外键指向商店或单位商店。

class Product(models.Model):

    product = models.CharField(max_length=200)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    store= models.ForeignKey(
        Store,
        related_name='products',
        on_delete=models.CASCADE,
    )