Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add unique sku in product

Michal Ociepkae1b2d5b9ed06
diff --git a/product/migrations/0005_auto__add_field_product_sku.py b/product/migrations/0005_auto__add_field_product_sku.py
new file mode 100644
index 0000000000..8ce1bb1b20
--- /dev/null
+++ b/product/migrations/0005_auto__add_field_product_sku.py
@@ -0,0 +1,59 @@
+# -*- coding: utf-8 -*-
+import datetime
+from south.db import db
+from south.v2 import SchemaMigration
+from django.db import models
+
+
+class Migration(SchemaMigration):
+
+ def forwards(self, orm):
+ # Adding field 'Product.sku'
+ db.add_column(u'product_product', 'sku',
+ self.gf('django.db.models.fields.CharField')(default='', max_length=32),
+ keep_default=False)
+
+
+ def backwards(self, orm):
+ # Deleting field 'Product.sku'
+ db.delete_column(u'product_product', 'sku')
+
+
+ models = {
+ u'product.category': {
+ 'Meta': {'object_name': 'Category'},
+ 'description': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+ u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'level': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
+ 'lft': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
+ 'parent': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'children'", 'null': 'True', 'to': u"orm['product.Category']"}),
+ 'rght': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
+ 'slug': ('django.db.models.fields.SlugField', [], {'unique': 'True', 'max_length': '50'}),
+ 'tree_id': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'})
+ },
+ u'product.digitalship': {
+ 'Meta': {'object_name': 'DigitalShip', '_ormbases': [u'product.Product']},
+ u'product_ptr': ('django.db.models.fields.related.OneToOneField', [], {'to': u"orm['product.Product']", 'unique': 'True', 'primary_key': 'True'}),
+ 'url': ('django.db.models.fields.URLField', [], {'max_length': '200'})
+ },
+ u'product.product': {
+ 'Meta': {'object_name': 'Product'},
+ 'category': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'products'", 'to': u"orm['product.Category']"}),
+ u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
+ 'price': ('django_prices.models.PriceField', [], {'currency': "'USD'", 'max_digits': '12', 'decimal_places': '4'}),
+ 'sku': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '32'})
+ },
+ u'product.ship': {
+ 'Meta': {'object_name': 'Ship', '_ormbases': [u'product.Product']},
+ 'depth': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0', 'blank': 'True'}),
+ 'length': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0', 'blank': 'True'}),
+ u'product_ptr': ('django.db.models.fields.related.OneToOneField', [], {'to': u"orm['product.Product']", 'unique': 'True', 'primary_key': 'True'}),
+ 'stock': ('django.db.models.fields.DecimalField', [], {'default': "'1'", 'max_digits': '10', 'decimal_places': '4'}),
+ 'weight': ('django.db.models.fields.PositiveIntegerField', [], {}),
+ 'width': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0', 'blank': 'True'})
+ }
+ }
+
+ complete_apps = ['product']
\ No newline at end of file
diff --git a/product/migrations/0006_update_sku.py b/product/migrations/0006_update_sku.py
new file mode 100644
index 0000000000..2224a3394a
--- /dev/null
+++ b/product/migrations/0006_update_sku.py
@@ -0,0 +1,56 @@
+# -*- coding: utf-8 -*-
+import datetime
+from south.db import db
+from south.v2 import DataMigration
+from django.db import models
+
+class Migration(DataMigration):
+
+ def forwards(self, orm):
+ Product = orm['product.product']
+ for product in Product.objects.all():
+ product.sku = str(product.id)
+ product.save()
+
+ def backwards(self, orm):
+ "Write your backwards methods here."
+
+ models = {
+ u'product.category': {
+ 'Meta': {'object_name': 'Category'},
+ 'description': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+ u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'level': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
+ 'lft': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
+ 'parent': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'children'", 'null': 'True', 'to': u"orm['product.Category']"}),
+ 'rght': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
+ 'slug': ('django.db.models.fields.SlugField', [], {'unique': 'True', 'max_length': '50'}),
+ 'tree_id': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'})
+ },
+ u'product.digitalship': {
+ 'Meta': {'object_name': 'DigitalShip', '_ormbases': [u'product.Product']},
+ u'product_ptr': ('django.db.models.fields.related.OneToOneField', [], {'to': u"orm['product.Product']", 'unique': 'True', 'primary_key': 'True'}),
+ 'url': ('django.db.models.fields.URLField', [], {'max_length': '200'})
+ },
+ u'product.product': {
+ 'Meta': {'object_name': 'Product'},
+ 'category': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'products'", 'to': u"orm['product.Category']"}),
+ u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
+ 'price': ('django_prices.models.PriceField', [], {'currency': "'USD'", 'max_digits': '12', 'decimal_places': '4'}),
+ 'sku': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '32'})
+ },
+ u'product.ship': {
+ 'Meta': {'object_name': 'Ship', '_ormbases': [u'product.Product']},
+ 'depth': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0', 'blank': 'True'}),
+ 'length': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0', 'blank': 'True'}),
+ u'product_ptr': ('django.db.models.fields.related.OneToOneField', [], {'to': u"orm['product.Product']", 'unique': 'True', 'primary_key': 'True'}),
+ 'stock': ('django.db.models.fields.DecimalField', [], {'default': "'1'", 'max_digits': '10', 'decimal_places': '4'}),
+ 'weight': ('django.db.models.fields.PositiveIntegerField', [], {}),
+ 'width': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0', 'blank': 'True'})
+ }
+ }
+
+ complete_apps = ['product']
+ symmetrical = True
diff --git a/product/migrations/0007_auto__add_unique_product_sku.py b/product/migrations/0007_auto__add_unique_product_sku.py
new file mode 100644
index 0000000000..da6b23d454
--- /dev/null
+++ b/product/migrations/0007_auto__add_unique_product_sku.py
@@ -0,0 +1,57 @@
+# -*- coding: utf-8 -*-
+import datetime
+from south.db import db
+from south.v2 import SchemaMigration
+from django.db import models
+
+
+class Migration(SchemaMigration):
+
+ def forwards(self, orm):
+ # Adding unique constraint on 'Product', fields ['sku']
+ db.create_unique(u'product_product', ['sku'])
+
+
+ def backwards(self, orm):
+ # Removing unique constraint on 'Product', fields ['sku']
+ db.delete_unique(u'product_product', ['sku'])
+
+
+ models = {
+ u'product.category': {
+ 'Meta': {'object_name': 'Category'},
+ 'description': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+ u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'level': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
+ 'lft': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
+ 'parent': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'children'", 'null': 'True', 'to': u"orm['product.Category']"}),
+ 'rght': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
+ 'slug': ('django.db.models.fields.SlugField', [], {'unique': 'True', 'max_length': '50'}),
+ 'tree_id': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'})
+ },
+ u'product.digitalship': {
+ 'Meta': {'object_name': 'DigitalShip', '_ormbases': [u'product.Product']},
+ u'product_ptr': ('django.db.models.fields.related.OneToOneField', [], {'to': u"orm['product.Product']", 'unique': 'True', 'primary_key': 'True'}),
+ 'url': ('django.db.models.fields.URLField', [], {'max_length': '200'})
+ },
+ u'product.product': {
+ 'Meta': {'object_name': 'Product'},
+ 'category': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'products'", 'to': u"orm['product.Category']"}),
+ u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
+ 'price': ('django_prices.models.PriceField', [], {'currency': "'USD'", 'max_digits': '12', 'decimal_places': '4'}),
+ 'sku': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '32'})
+ },
+ u'product.ship': {
+ 'Meta': {'object_name': 'Ship', '_ormbases': [u'product.Product']},
+ 'depth': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0', 'blank': 'True'}),
+ 'length': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0', 'blank': 'True'}),
+ u'product_ptr': ('django.db.models.fields.related.OneToOneField', [], {'to': u"orm['product.Product']", 'unique': 'True', 'primary_key': 'True'}),
+ 'stock': ('django.db.models.fields.DecimalField', [], {'default': "'1'", 'max_digits': '10', 'decimal_places': '4'}),
+ 'weight': ('django.db.models.fields.PositiveIntegerField', [], {}),
+ 'width': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0', 'blank': 'True'})
+ }
+ }
+
+ complete_apps = ['product']
\ No newline at end of file
diff --git a/product/models.py b/product/models.py
index fb2cb51de3..23205631b5 100644
--- a/product/models.py
+++ b/product/models.py
@@ -104,8 +104,9 @@ class Product(Subtyped, Item):
price = PriceField(
pgettext_lazy('Product field', 'price'),
currency='USD', max_digits=12, decimal_places=4)
- category = models.ForeignKey(
- Category, related_name='products',
+ sku = models.CharField(pgettext_lazy('Product field', 'sku'),
+ max_length=32, unique=True)
+ category = models.ForeignKey(Category, related_name='products',
verbose_name=pgettext_lazy('Product field', 'category'))
def __unicode__(self):