admin管理员组文章数量:1401849
I have a wagtail site and I want to create an object hierarchy in a one-to-one matter, but with multiple options. Basically, I want that the database setup look slike this:
CREATE TABLE products (
id PRIMARY KEY,
product_type VARCHAR(10) CHECK (product_type IN ('BOOK', 'SHIRT', ...)),
product_name VARCHAR(255),
product_description TEXT,
...
);
CREATE TABLE product_shirts (
id PRIMARY KEY,
product_id integer REFERENCES products (id),
size varchar(255),
...
);
CREATE TABLE product_books (
id PRIMARY KEY,
product_id integer REFERENCES products (id),
author varchar(255),
...
);
It is pretty straigt forward to create a regular one-to-one relationship with setting ParentalKey in the derived model. However, I want to also have an enum-type field in the parent model to check which product type we have, so that I can do something like that in my ProductsView:
if object.product_type == 'SHIRT':
# display additional shirt attributes
elif object.product_type == 'BOOK':
# display book attributes
else:
# unknown type, should not happen
I know, that with a one-to-one relationship in wagtail I could just simply call product.shirt
which would raise an exception, if the product is not a shirt. But it seems very cumbersome to have nested try-catch blocks if I have many different product types...
Any better idea to solve this in a django/wagtail style?
本文标签: pythonWagtail onetoone object hierarchy with enum type fieldStack Overflow
版权声明:本文标题:python - Wagtail one-to-one object hierarchy with enum type field - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744316563a2600293.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论