简体   繁体   English

数据库和类设计Django

[英]Database and class design Django

I am building a website for selling bikes in django. 我正在建立一个在Django出售自行车的网站。 I have a class Bike that will contain the bikes; 我有一个班级的自行车,里面要放自行车。 it looks like this : 它看起来像这样:

class Bike(models.Model):
    brand = models.CharField(max_length=100)
    model = models.CharField(max_length=100)

Now I would like to add a field wheels describing the wheels of the bike, and I would like this field to contain possibly several fields like the brand, size of the wheel. 现在,我想添加一个描述自行车车轮的场轮,我希望该场可能包含几个领域,例如品牌,轮毂尺寸。 I would like these details on wheels implementation to be separated from the bike's class specification; 我希望将有关车轮实现的这些细节与自行车的等级规范分开; however, I expect each wheel to be associated to exactly one bike. 但是,我希望每个车轮都恰好与一辆自行车相关联。

An idea I had was to do the following : 我的想法是执行以下操作:

class Wheels(models.Model):
    description = models.CharField(max_length=100)
    size = models.DecimalField(max_digits=5,decimal_places=2)

and then to include a new field in my bike : 然后在我的自行车中加入一个新领域:

class Bike(models.Model):
    # previous fields
    wheels = models.ForeignKey(Wheels)

I have however some doubts about it : 但是我对此有些怀疑:

1) is it the correct design ? 1)这是正确的设计吗? If I do that I will end up with a wheels' database which I don't think I actually need. 如果这样做,我将得到一个我实际上不需要的车轮数据库。 I just want to have flexible fields in my bike database. 我只想在我的自行车数据库中拥有灵活的字段。 Basically I expect that I will have a one to one relationship between bikes and wheels. 基本上,我希望我会在自行车和车轮之间建立一对一的关系。

2) If this is the correct design, then what I would like is to be able to add wheels on the fly while adding bike (never have to add wheels separately). 2)如果这是正确的设计,那么我希望能够在添加自行车的同时即时添加轮子(不必分别添加轮子)。 What is the best way to do that ? 最好的方法是什么?

Thanks a lot for any hint / reference. 非常感谢您的任何提示/参考。 I am a beginner with django... 我是django的初学者...

I doubt that (in real life) you really will have a one-to-one relationship between bike and wheels - more than one bike model will surely use the same wheels. 我怀疑(在现实生活中)您是否真的会在自行车和车轮之间建立一对一的关系-肯定有多个自行车模型会使用相同的车轮。

Similar real life relationships exist between bike brands/models and components such as derailleurs, brakes, cranks, pedals etc. 自行车品牌/型号与部件之间存在类似的现实关系,例如拨链器,制动器,曲柄,踏板等。

By the way, you would not have separate databases for each component, each component would be modelled as a table within the same database. 顺便说一下,您不会为每个组件拥有单独的数据库 ,每个组件都将被建模为同一数据库内的一个

So my advice is to go with the multiple table approach because you will eventually have the same components used on different bikes, and possibly vice versa for cases where there are optional components within the same basic bike model, eg same bike but different wheel sizes. 因此,我的建议是采用多表方法,因为最终您将拥有在不同自行车上使用的相同组件,对于相同基本自行车模型中存在可选组件(例如,相同自行车但车轮尺寸不同)的情况,反之亦然。

  1. That design looks good to me - it's a good idea to keep individual objects in separate tables (in this case the components that make up the bike - particularly since they can be sold separately, and bikes can be customised with different parts) 这种设计对我来说看起来不错-将单个对象放在单独的桌子中是个好主意(在这种情况下,构成自行车的组件特别是因为它们可以单独出售,并且可以用不同的零件定制自行车)

  2. I think a simple preset class inherited from the Bicycle class should do the trick: 我认为,从Bicycle类继承的简单预设类应该可以解决问题:

     class BicycleFrame(models.Model): brand = models.ForeignKey(Brand) model = models.CharField(max_length=100) # class BicycleWheels, BicyclePedals etc.. class Bicycle(models.Model): frame = models.ForeignKey(BicycleFrame) wheels = models.ForeignKey(BicycleWheels) pedals = models.ForeignKey(BicyclePedals) # etc ... class PresetBicycle(Bicycle): pass class PurchaseableMixin(models.Model): user_id = models.ForeignKey(Customer) def purchase(self): # ... call this in the form handler to save this # instance of a bike in the database and maybe # email the customer to inform them of their purchase etc.. class Meta: abstract = True class PurchasedBicycle(Bicycle, PurchaseableMixin): pass 

.. then you can create a PresetBicycle in your admin area then in the view that you show to customers you could display the PresetBicycle by default and also provide a form to purchase it that is auto filled with the details for the PresetBicycle and that creates an instance of a PurchasedBicycle on submission (a JS framework like ReactJS and Backbone, or Angular might be best for the customer view). ..然后您可以在管理区域中创建PresetBicycle,然后在向客户显示的视图中,默认情况下可以显示PresetBicycle并提供购买表单,该表单会自动填充PresetBicycle的详细信息并创建一个提交时的PurchasedBicycle实例(像ReactJS和Backbone这样的JS框架,或者Angular可能是最适合客户视图的)。

Hope this helps! 希望这可以帮助!

PS Note that I haven't tested this approach myself - I'd suggest creating a few new branches in your Version Control (eg git) and setting up separate settings files for each one (import from a base settings file and use a separate database for each one to save having to mess around with too many migrations and obsolete tables) to test several approaches before making your final decision - it's good to figure this stuff out early on so that you don't end up making big structural changes later on. PS注意,我自己尚未测试过这种方法-建议您在Version Control中创建一些新分支(例如git),并为每个分支设置单独的设置文件(从基本设置文件导入并使用单独的数据库)这样一来,每个人就省去了太多的迁移和过时的表),可以在做出最终决定之前先测试几种方法-最好尽早弄清这些东西,以免以后再进行大的结构更改。

PPS Also not that I've changed the brand field to be a ForeignKey, since you may later wish to filter on the brand.. PPS另外,我还没有将品牌字段更改为ForeignKey,因为您以后可能希望过滤该品牌。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM