Editing models and extending database structure in Saleor

Viewed 1032

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!

1 Answers

The django way to create new models, (and Saleor“s backend is django based) is:

  1. You should create a new app on your store backend (the django part of saleor) with:

    $ python manage.py startapp artist

  2. Create your Artist model, with all the fields you want such as email, etc... in the file: artist/models.py.
  3. Modify the Product model in the file product/models.py by importing the Artist models and adding a ForeignKey (for example) relationship to it.
  4. Register the new artist app in your settings.py's "INSTALLED_APPS".
  5. Run python manage.py makemigrations... (Check they include your changes to models)
  6. 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:

Related