Django数据库模型:字段取决于对象的类型(Django database model: fields depending on the type of the object)

编程入门 行业动态 更新时间:2024-10-28 02:29:19
Django数据库模型:字段取决于对象的类型(Django database model: fields depending on the type of the object)

我有一些将用于同一庄园的物品。 此对象分类为此对象的预定义类型。 每种类型都具有属于此类对象的属性。 为了在模型中得到这个,我想到了这个模型表:

一个表格,包含对象的类型 一个表具有属于该类型的预定义属性(具有该字段的描述/帮助文本,是要求的属性,字段的类型等) 一个表,其中的对象与表的关系及其属性(此列表中的下一个) 一个表,其中包含对象的属性及其值(请参阅其定义的预定义属性)

它确实有效,但需要一些编程来保持一致。

此解决方案的另一个问题是属性的字段类型,这可能不同。 所以,或者我将使用1字段类型(charField)并尝试解析它,或者我将拥有一个具有不同值字段的表,其中使用最多的字段类型(value_integer = models.IntegerField)。 在这种情况下,您可以获得大量的emtpy字段,只需要为每个属性填充1个字段。 其实我在寻找某种字典......

不确定这是否是正确的方法....任何建议?

I have some objects which will be used in the same manor. This object is categorised in predefined types of this object. Each type has properties belonging to this type of the object. To get this in a model, I had this tables for the model in mind:

One table with the types of the object One table with the predefined properties belonging to the type (with a description/help text of the field, is the property requierd, the type of the field and so on) One table with the objects with a relation to the table with its properties (the next one in this list) One table with the properties of the objects and it values (refering to the predefined properties for its definition)

it does work, but needs some programming to keep things together.

another issue with this solution is the fieldtype of the property, this can be different. So or I'll use 1 field type (charField) and try to parse it, or I'll have a table with different value fields with the most used field types (value_integer = models.IntegerField). In this case you get a lot of emtpy fields just to have 1 of the fields filled in per property. Actually I Am looking for some kind of dictionary...

Not sure whether this is the right way to go....any suggestions?

最满意答案

Django的方法是为每个对象类型定义一个模型类,正如所有Django的功能(管理,表单,外部应用程序......)所期望的那样。

您可以定义超类并使用模型继承 ,例如:

class BaseThing(models.Model): # fields/relations for all types of things class FooThing(BaseThing): # fields/relations for things of type "foo" class BarThing(BaseThing): # fields/relations for things of type "bar"

在那里,您可以照常预定义默认值 。

如果您希望您的用户在管理员中创建他们的类,那么请考虑另一个框架(例如, eZ Publish适合此需求)。

Django's way to go is to define one model class per object type, as expected by all Django's features (admin, forms, external apps ...).

You can define a superclass and use model inheritance, e.g:

class BaseThing(models.Model): # fields/relations for all types of things class FooThing(BaseThing): # fields/relations for things of type "foo" class BarThing(BaseThing): # fields/relations for things of type "bar"

There, you can predefine defaults as usual.

If you want your user to create their classes in the admin then consider another framework (eZ Publish suits this need for example).

更多推荐

本文发布于:2023-08-04 04:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1409315.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   模型   对象   类型   数据库

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!