Skip to content

Commit

Permalink
Merge pull request #165 from MySecondLanguage/added-product-attribute
Browse files Browse the repository at this point in the history
Added product attribute
  • Loading branch information
MySecondLanguage authored Oct 11, 2024
2 parents 07a81ea + dee19d3 commit 2b4f941
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 4.2.11 on 2024-10-11 10:06

from django.db import migrations, models
import nxtbn.core.models


class Migration(migrations.Migration):

dependencies = [
('product', '0007_productvariant_dimensions_and_more'),
]

operations = [
migrations.RemoveField(
model_name='productvariant',
name='internal_metadata',
),
migrations.RemoveField(
model_name='productvariant',
name='metadata',
),
migrations.AddField(
model_name='productvariant',
name='attributes',
field=models.JSONField(blank=True, default=dict, help_text='A JSON field to store custom attributes for the variant. Primary attributes include color code, dimension, and weight value. Weight value and dimension are used for shipping calculations. This field allows adding as many custom attributes as needed.', null=True, validators=[nxtbn.core.models.no_nested_values]),
),
]
19 changes: 15 additions & 4 deletions nxtbn/product/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from nxtbn.core import CurrencyTypes, MoneyFieldTypes
from nxtbn.core.mixin import MonetaryMixin
from nxtbn.core.models import AbstractMetadata, AbstractSEOModel, AbstractUUIDModel, PublishableModel, AbstractBaseUUIDModel, AbstractBaseModel, NameDescriptionAbstract
from nxtbn.core.models import AbstractMetadata, AbstractSEOModel, AbstractUUIDModel, PublishableModel, AbstractBaseUUIDModel, AbstractBaseModel, NameDescriptionAbstract, no_nested_values
from nxtbn.filemanager.models import Document, Image
from nxtbn.product import DimensionUnits, StockStatus, WeightUnits
from nxtbn.tax.models import TaxClass
Expand Down Expand Up @@ -177,7 +177,7 @@ def get_absolute_url(self):
return reverse("product_detail", args=[self.slug])


class ProductVariant(MonetaryMixin, AbstractUUIDModel, AbstractMetadata, models.Model):
class ProductVariant(MonetaryMixin, AbstractUUIDModel, models.Model):
money_validator_map = {
"price": {
"currency_field": "currency",
Expand Down Expand Up @@ -212,9 +212,11 @@ class ProductVariant(MonetaryMixin, AbstractUUIDModel, AbstractMetadata, models.
# if track_inventory is not enabled
stock_status = models.CharField(default=StockStatus.IN_STOCK, choices=StockStatus.choices, max_length=15)

color_code = models.CharField(max_length=7, null=True, blank=True)

sku = models.CharField(max_length=50, unique=True)


color_code = models.CharField(max_length=7, null=True, blank=True)
# Weight and dimensions are also types of attributes, but we created these fields separately for shipping rate calculation purposes.
weight_unit = models.CharField(
max_length=5,
choices=WeightUnits.choices,
Expand All @@ -237,6 +239,15 @@ class ProductVariant(MonetaryMixin, AbstractUUIDModel, AbstractMetadata, models.
dimensions_value = models.CharField(max_length=50, blank=True, null=True)


attributes = models.JSONField(
blank=True,
null=True,
default=dict,
validators=[no_nested_values],
help_text="A JSON field to store custom attributes for the variant. Primary attributes include color code, dimension, and weight value. Weight value and dimension are used for shipping calculations. This field allows adding as many custom attributes as needed."
)



class Meta:
ordering = ('price',) # Order by price ascending
Expand Down

0 comments on commit 2b4f941

Please sign in to comment.