Django和eBay风格的多重上市 [英] Django and eBay style multi listing

查看:67
本文介绍了Django和eBay风格的多重上市的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试模仿eBay的多重列表功能。基本上,eBay允许用户指定他们选择的两种变体,例如 size和color 。每个变体将具有不同的数据,例如:

 颜色:红色,绿色
大小:12、9

然后它会引起一些混乱,例如:

 颜色红色的数量,大小12:15 
数量红色,数量9:12
数量颜色绿色,大小12:20
数量对于绿色,大小9:59

用户将必须指定每种混合变化的数量,第一个版本优先。



使事情复杂化姓名和颜色仅是示例。它们可以是不同的属性。



如何在数据库级别应用它?目前,我只是在生成类似这样的列表:

  [颜色蓝色,尺寸= 12,数量= 24,价格= 299 ,size = 23,Quantity = 43,Price = 298] 

计划将列表存储为字符串一个字段,并使用JSON重建它。



问题是,每个请求都将需要不必要的处理。例如,如果数量减少,那么我不能只编辑一个字段,就必须使用标志从列表中找到正确的数量(不必要的处理),并对列表进行适当的更改,然后再次存储。



寻找替代方法?我无法创建变化字段,因为它们是由用户指定的。



正在寻找某个方向。



我当前的表格如下:

  class Auction(models.Model):
auction_id = models.IntegerField( primary_key = True)
名称= models.CharField(max_length = 50)
描述= models.TextField(validators = [MaxLengthValidator(1000)])
价格= models.PositiveSmallIntegerField(默认= 0 )
bid = models.PositiveSmallIntegerField(默认= 0)
image = models.ImageField(upload_to ='img /',默认='img / None / no-img.jpg')
......
#存储多个列表/字典
multi_listing = models.CharField(max_length = 200)

flag = models.BooleanField(default = False)
slug = models.SlugField(unique = True,默认=(randint(0,1000000)))

def __unicode __(self):
return self.name


解决方案

JSON




计划将列表作为字符串存储在字段中,并使用JSON重建它。


如果完全采用这种方法,则应该使用具有本地JSON字段的数据库。在Django官方支持的数据库中,只有Mysql和Postgresql拥有它。不幸的是,Django不完全支持mysql版本,您将需要第三方库。



两者中,postgresql具有更丰富的功能,并提供了更好的索引选项。实际上,postgresql中的JSONB足以与Mongodb竞争。



如果您选择JSON,则必须使用Postgresql JSONB。没有什么比这更接近了。使用JSONB,不需要重建任何内容,也不需要获取完整的字段即可更新JSON字典中的一项。



但是数据您选择的结构是错误的。

  [{'color':'blue','size':12,'Quantity':' 24','Price':299},
{'size':23,'Quantity':43,'Price':298}]

但是,还是,仍然数组


提示:未设置数组;搜索特定的数组元素可能是数据库设计错误的标志。考虑使用一个单独的表,该表的每个行都有
行,这将是一个数组元素。
会更容易搜索,并且可能会针对大量
元素进行更好地扩展。


尽管如此,JSON数组比ArrayField更有效



相关模型



这样的方式。并且可能比使用JSON更好,如果不使用postgresl,则绝对更好。您的模型可能看起来像这样:

  class Auction(models.Model):
auction_id = models.IntegerField(primary_key = True)
名称= models.CharField(max_length = 50)
描述= models.TextField(validators = [MaxLengthValidator(1000)])

bid = models.PositiveSmallIntegerField(默认= 0)
image = models.ImageField(upload_to ='img /',默认='img / None / no-img.jpg')

标志= models.BooleanField(默认= False)
slug = models.SlugField(unique = True,默认=(randint(0,1000000)))

def __unicode __(self):
返回self.name


class Pricing(models.Model):
#存储多个列表/字典

价格= models.PositiveSmallIntegerField(默认= 0)
color = models.CharField(max_length = 12)
size = models.IntegerField(max_length = 200)
auction = models.ForeignKey(Auction)



更新




因为颜色和大小是由用户定义的,因此可以将其命名为
。例如,他们可能选择其他变体
而不是'size'和'color',他们可能会选择'material'和
'color',那么如何命名字段?


此说明使规模偏向JSON。但是,仍然可以考虑其他两种选择。首先是redis。不要将redis完全替代RBMS,而是将用户定义的属性存储在redis中。



第二种方法是完全切换到nosql数据库,例如mongo。不幸的是,尽管Django在nosql方面表现不佳。


I'm trying to mimic eBay's multi listing function. Basically, eBay allows users to specify two variations of their choice, for example size and color. Each variation will have different data, for example:

Color: red, green
Size: 12, 9

Then it gets a bit confusing for example:

quantity for color red, size 12: 15
quantity for color red, size 9: 12
quantity for color green, size 12: 20
quantity for color green, size 9: 59

The user will have to specify the quantity for each mixture of variation, first variation takes precedence.

To complicate matters Name and Color are examples only. They can be different properties.

How can I apply this in database level? Currently I'm just producing a list like so:

[color blue,size = 12,Quantity = 24,Price = 299,size = 23,Quantity = 43,Price = 298]

Planning to store the list as string in a field, and using JSON to rebuild it.

The problem is, unnecessary processing will be required for every request. For example if Quantity decreases, then I can't just edit a field, I would have to use flags to find right Quantity from the list (unnecessary processing), and make appropriate changes to the list, and then store it again.

Looking for an alternative approach? I can't create fields for variations because they're specified by the user.

Looking for some direction.

My current Table looks like:

class Auction(models.Model):
    auction_id = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=50)
    description = models.TextField(validators=[MaxLengthValidator(1000)])
    price = models.PositiveSmallIntegerField(default = 0)
    bid = models.PositiveSmallIntegerField(default = 0)
    image = models.ImageField(upload_to = 'img/', default = 'img/None/no-img.jpg') 
    ......
    # stores multi listing list/dict
    multi_listing = models.CharField(max_length=200)

    flag = models.BooleanField(default=False)
    slug = models.SlugField(unique=True, default = (randint(0,1000000)))

    def __unicode__(self):
        return self.name

解决方案

JSON

Planning to store the list as string in a field, and using JSON to rebuild it.

If you take this approach at all, you should be using a database that has a native JSON field. Of the databases that are officially supported by django only Mysql and Postgresql has it. Unfortunately the mysql version isn't fully supported by Django and you will need third party libraries.

Of the two, postgresql has the richer set of functionality and provides better indexing options. In fact, JSONB in postgresql is good enough to rival Mongodb.

If JSON is your choice, it has to be Postgresql JSONB. Nothing else will come even close. With JSONB there is no need to rebuild anything and you don't need to fetch the complete field just to update one of the items in the JSON dictionary.

But the data structure you have chosen is wrong. It should be more like

[{'color': 'blue' , 'size': 12, 'Quantity': '24', 'Price': 299}, 
 {'size': 23, 'Quantity': 43, 'Price' :298}]

But oops, that's still an Array

Tip: Arrays are not sets; searching for specific array elements can be a sign of database misdesign. Consider using a separate table with a row for each item that would be an array element. This will be easier to search, and is likely to scale better for a large number of elements.

Nevertheless, a JSON array can be more effective than an ArrayField

Related Model

This is the more traditional way of doing this. And probably better than using JSON, definitely better if you are not using postgresl. Your model might look like this:

class Auction(models.Model):
    auction_id = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=50)
    description = models.TextField(validators=[MaxLengthValidator(1000)])

    bid = models.PositiveSmallIntegerField(default = 0)
    image = models.ImageField(upload_to = 'img/', default = 'img/None/no-img.jpg') 

    flag = models.BooleanField(default=False)
    slug = models.SlugField(unique=True, default = (randint(0,1000000)))

    def __unicode__(self):
        return self.name


class Pricing(models.Model):
    # stores multi listing list/dict

    price = models.PositiveSmallIntegerField(default = 0)
    color = models.CharField(max_length=12)
    size = models.IntegerField(max_length=200)
    auction = models.ForeignKey(Auction)

Update

because 'color' and 'size' is defined by user, it could be name something else. For example they may choose a different variation instead of 'size' and 'color', they might choose 'material' and 'color', so then how would you name the field?

This explaination tilts the scales in favor of JSON. However there is still two other alternatives that could possibly be considered. The first is redis. Not to use redis as a complete replacement for your RBMS but to store the user defined properties in redis.

The second alternative is to completey switch to a nosql database such as mongo. Unfortunately though Django doesn't play very well with nosql.

这篇关于Django和eBay风格的多重上市的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆