在 Saleor 中编辑模型和扩展数据库结构 [英] Editing models and extending database structure in Saleor
问题描述
我最近为一个网络应用程序创建了 Saleor 2.9,我正在为一个艺术画廊构建一个网络应用程序,该应用程序想要展示他们待售的产品并为他们的艺术家提供一些宣传.我希望能够拥有一堆卡片(如我们的团队"组件),从后端的艺术家表中提取数据,该表存储有关艺术家姓名、电子邮件、出身等信息,然后显示它在前端.我正在努力查看如何修改模型/数据库以创建一个包含姓名、电子邮件、信息的新艺术家"表,然后与我在 DC 中填充的产品创建类似 manyToMany 的关系,为产品提供创建者"属性./dashboard 目录中有大量模型文件,即使我对核心模型进行更改以创建艺术家类,我也不知道如何将其显示在仪表板上,以便可以从中创建/修改艺术家那里.
我想让客户(非技术人员)可以添加艺术家并让他们出现在我将制作的艺术家页面上,有点像产品出现在他们的页面上(但显然我不能创建一个新的艺术家"类别,因为艺术家不能有价格或运费,因为他们是人;还有我想要的其他属性,例如产品不能具有的电子邮件.它们也与网站上的工作人员不同,所以我不能使用员工管理"功能.)
我查看了这个 问题,但从那时起,Saleor 的结构发生了变化,这是对现有类(用户)的相对较小的属性更改,而不是新类的创建和集成.令我惊讶的是,尽管广泛搜索有关如何做一些像创建新模型这样简单的事情,但在线文档和讨论很少;我肯定错过了什么.
请帮忙:)谢谢!
创建新模型的 django 方式(并且 Saleor 的后端是基于 django 的)是:
- 你应该在你的商店后端(saleor 的 django 部分)上创建一个新的应用程序:<块引用>
$ python manage.py startapp 艺术家
- 创建您的艺术家模型,在文件中包含您想要的所有字段,例如电子邮件等...:
artist/models.py
. - 通过导入艺术家模型并向其添加外键(例如)关系来修改文件
product/models.py
中的产品模型. - 在您的
settings.py
的INSTALLED_APPS"中注册新的艺术家应用. - 运行
python manage.py makemigrations
...(检查它们包括您对模型的更改) - 运行
python manage.py migrate
.
应该是这样.'少我忘记了一些东西,在这种情况下,请在您进行此操作后回帖.
<小时>注意事项:
- 您可能想先备份您的数据库.
- 此外,在应用这些迁移时,django 会要求您提供产品的占位符值,这些产品在产品之前的数据库中有艺术家字段.
参考:
I recently forked Saleor 2.9 for a web app I am building for an art gallery that wants to display their products for sale as well as give their artists some publicity. I want to be able to have a bunch of cards (like "our team" components) that pull data from an Artists table on the back-end that stores information about the artists' names, emails, origins, etc, and then display it on the front-end. I am struggling to see how to modify the models/DB to create a new "Artists" table with name, email, info, and then to create a manyToMany-like relationship with the products I've populated in the DC, giving the products a "created by" attribute. There are tons of models files throughout the /dashboard directory, and even when I make changes to the core models to create an artist class, I don't know how to get it to show on the dashboard so artists can be created/modified from there.
I would like to make it so that the client (non-technical) can add artists and have them show up on the artists page I will make, somewhat like products show up on their pages (but obviously I cannot create a new category "Artist" as artists cannot have prices or shipping as they are people; and there are other attributes I would want like email that a product cannot have, either. They are also different to staff on the website, so I cannot use the "staff management" functionality.)
I looked at this question but Saleor structure has changed since then, and that was a relatively minor attributal change to an existing class (User) as opposed to the creation and integration of a new class. I'm surprised that despite extensively searching for anything on how to do something as straightforward as create a new model there is little documentation and discussion online; I must be missing something.
Please help :) Thank you!
The django way to create new models, (and Saleor´s backend is django based) is:
- You should create a new app on your store backend (the django part of saleor) with:
$ python manage.py startapp artist
- Create your Artist model, with all the fields you want such as email, etc... in the file:
artist/models.py
. - Modify the Product model in the file
product/models.py
by importing the Artist models and adding a ForeignKey (for example) relationship to it. - Register the new artist app in your
settings.py
's "INSTALLED_APPS". - Run
python manage.py makemigrations
... (Check they include your changes to models) - Run
python manage.py migrate
.
That should be it. 'less I`m forgeting something, in which case, please post back when you have moved forward with this.
Notes:
- You may want to back up your DB first.
- Also when applying these migrations, django will ask you for a placeholder value for products which where in your DB before Product had an Artist field.
References:
这篇关于在 Saleor 中编辑模型和扩展数据库结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!