question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to have multiple product variant models?

See original GitHub issue

Hi,

I have some trouble grasping the concept of modeling a catalog with different variant requirements. How would one create a catalog with multiple ProductVariant models?

For example if I would have both mobile phones and t-shirts in a catalogue:


# Shirt product
class Shirt(ProductBase):
    description = RichTextField()
    # Other shirt fields

    content_panels = ProductBase.content_panels + [
        FieldPanel('description'),
        InlinePanel('variants', label='Product variants'),
    ]

class ShirtVariant(ProductVariantBase):
    COLORS = [('red', 'Red'), ('green', 'Green'), ('blue', 'Blue')]
    product = ParentalKey(Shirt, related_name='variants')
    shirt_color = models.CharField(choices=COLORS, default='blue')


# Phone product
class Phone(ProductBase):
    description = RichTextField()
    # Other phone fields

    content_panels = ProductBase.content_panels + [
        FieldPanel('description'),
        InlinePanel('variants', label='Product variants'),
    ]

class PhoneVariant(ProductVariantBase):
    CAPACITIES = [('32', '32 G'), ('64', '64 G'), ('128', '128 G')]
    product = ParentalKey(Phone, related_name='variants')
    phone_capacity = models.CharField(choices=CAPACITIES, default='32')

I have noticed that PRODUCT_VARIANT_MODEL setting is required, and I realise this is not the intended use of the ProductVariant model. Reading through the docs I’ve failed to understand how to achieve a similar catalog model.

Any explanation would be much appreciated. Thanks =)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
JamesRammcommented, Dec 18, 2019

Having re-read the initial question, I think this is just a case of making your variant models more dynamic.

There isn’t any difference between a Shirt product and a Phone in how you have defined it, so we can keep the standard product class:

class Product(ProductBase):
    description = RichTextField()

    content_panels = ProductBase.content_panels + [
        FieldPanel('description'),
        InlinePanel('variants', label='Product variants'),
    ]

we might want to enrich it with a ‘category’ field.

Now we want a variant model, where we can choose what we use for the ‘choices’. Django has something builtin for that - the ModelChoiceField

class ProductVariant(ProductVariantBase):

   product = ParentalKey(Product, related_name='variants')
   property_name = models.CharField(...)
   property_model = models.ModelChoiceField(...)

We can then make any number of separate models for different types of variants. The idea is to make the product/variant as generic and configurable as possible (balanced against ease of use) so that when you have a new product type you can just add it in the admin.

1reaction
thenewguycommented, Dec 16, 2019

@tboulogne @dinoperovic I have plans to be spending some time around this in a couple of months once some obligations are cleared. Please share any progress you made here and your approach. What made the implementation difficult for you? Is there anything in particular that would ease your troubles?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Design Guidelines for Selling Products with Multiple Variants
Different products should have different listings; product variations should be displayed under a single listing.
Read more >
Adding variants - Shopify Help Center
If your variants differ in more than one way, then tap Add another option. You can have up to 3 options for each...
Read more >
Modeling Product Variants - mysql - Stack Overflow
A product can have 0 or more product variants (e.g. a t-shirt product may have size and color variants). · A product variant...
Read more >
Products and variants | Tutorials
When deciding to use product variants, try multiplying the variable attributes with each other. This gives a rough idea of how many variants...
Read more >
How to Set Up Product Options - The Complete Guide to ...
Certain products need to have options in eCommerce, like apparel of different colors and sizes. Here's how to add product variations to your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found