Sof*_*tic 5 python django django-models django-admin
我有一个独特的问题,它应该在django admin中处理.
我有以下模型结构......
class Product(models.Model):
name = models.CharField(max_length = 100)
base_price = models.DecimalField(max_digits = 5, decimal_places = 2)
def __unicode__(self):
return self.name
class Country(models.Model):
name = models.CharField(max_length = 2)
base_price = models.DecimalField(max_digits = 5, decimal_places = 2)
def __unicode__(self):
return self.name
class CountryProduct(models.Model):
country = models.ForeignKey(Country)
product = models.ForeignKey(Product)
overriden_price = models.DecimalField(max_digits = 5, decimal_places = 2)
class Meta:
unique_together = (("country", "product"),)
Run Code Online (Sandbox Code Playgroud)
如图所示,产品和国家之间存在多种关系....我想提供管理界面以覆盖给定国家和产品的基本价格.
ui的一个选项如下,这里破折号( - )表示默认价格,数字值表示给定国家和产品的覆盖价格.
countries -> | US | UK
products | |
---------------------------
Product1 | - | 10
Product2 | 5 | 7
Run Code Online (Sandbox Code Playgroud)
但我不知道该怎么做......
我愿意看看替代方法(包括模型结构的变化)以及满足要求......你的任何形式的输入肯定对我有用......
提前致谢 :)
我找到了解决方案,这是我对问题的回答...让我与您分享...我按以下方式更改了模型...
class Product(models.Model):
name = models.CharField(max_length = 100)
base_price = models.DecimalField(max_digits = 5, decimal_places = 2)
def __unicode__(self):
return self.name
class Country(models.Model):
name = models.CharField(max_length = 2)
base_price = models.DecimalField(max_digits = 5, decimal_places = 2)
products = models.ManyToManyField(Product, through = 'CountryProduct')
def __unicode__(self):
return self.name
class CountryProduct(models.Model):
country = models.ForeignKey(Country)
product = models.ForeignKey(Product)
overriden_price = models.DecimalField(max_digits = 5, decimal_places = 2)
class Meta:
unique_together = (("country", "product"),)
class CountryProductInline(admin.TabularInline):
model = CountryProduct
class CountryAdmin(admin.ModelAdmin):
inlines = [CountryProductInline]
class ProductAdmin(admin.ModelAdmin):
inlines = [CountryProductInline]
Run Code Online (Sandbox Code Playgroud)
虽然这不是我期望的方式,但这给了我更好的解决方案......
| 归档时间: |
|
| 查看次数: |
2416 次 |
| 最近记录: |