python - Wagtail one-to-one object hierarchy with enum type field - Stack Overflow

I have a wagtail site and I want to create an object hierarchy in a one-to-one matter, but with multipl

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.shirtwhich 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?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744316563a4568212.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信