【发布时间】:2014-12-22 20:17:57
【问题描述】:
一方面,我有三个 CollectionsView(从服务器获取的三种类型的模型),另一方面,我有一个像购物车一样工作的四分之一 CollectionView。这存储了与视图关联的其他三个集合中的项目(集合视图)。
问题是当我添加一个具有相同 id 的项目时,这是被编辑的,而不是添加到购物车 CollectionView 中。
例子:
function addToCart(model){
ShoppingCartCollectionView.collection.add(model);
}
来自其他收藏:
// From one CollectionView
addToCart(this.model);
// From another CollectionView
addToCart(this.model);
此集合具有相同的 id,因为它们存储在服务器上的不同数据库中。
这是我的模型(Python ORM)
PRODUCT_TYPE = (
('EM', 'Empanada'),
('BE', 'Bebida'),
('OF', 'Oferta'),
)
# Create your models here.
class Producto(models.Model):
nombre = models.CharField(max_length=50)
precio_unidad = models.DecimalField(max_digits=8, decimal_places=2)
descripcion = models.TextField()
stock = models.IntegerField()
status = models.BooleanField(default=True)
imagen = models.ImageField(upload_to=upload_to)
fecha_publicacion = models.DateTimeField(auto_now_add=True)
class Meta:
abstract = True
class Empanada(Producto):
precio_docena = models.DecimalField(max_digits=8, decimal_places=2)
product_type = models.CharField(max_length=2, choices=PRODUCT_TYPE, default='EM', editable=False)
def __unicode__(self):
return self.nombre
class Bebida(Producto):
product_type = models.CharField(max_length=2, choices=PRODUCT_TYPE, default='BE', editable=False)
def __unicode__(self):
return self.nombre
class Oferta(Producto):
product_type = models.CharField(max_length=2, choices=PRODUCT_TYPE, default='OF', editable=False)
def __unicode__(self):
return self.nombre
class Venta(models.Model):
pedido = models.TextField()
total = models.DecimalField(max_digits=8, decimal_places=2)
status = models.BooleanField(default=True)
fecha_publicacion = models.DateTimeField(auto_now_add=True)
如何解决?
谢谢!!!
【问题讨论】:
-
您确定您的数据库结构设置正确吗?在我看来,来自
products数据库的产品都将具有唯一的 id (PK)。当有不同类型的产品时,您只需创建一个 FK 来指示 product_type;你不需要三个不同的表... -
感谢您的回答。我通过添加我的数据库来编辑帖子。是的,是多余的,但一种产品具有其他产品没有的属性。另一种模式?
标签: backbone.js collections views add repeat