Foreign key field with limit_choices_to

24 views
Skip to first unread message

dmbar...@gmail.com

unread,
Jun 4, 2014, 2:49:18 PM6/4/14
to django...@googlegroups.com
class ProductGroup(models.Model):
    group_sku = models.CharField(unique=True, max_length=255)
    brand = models.CharField(max_length=255)
    ...

class Product(models.Model):
    product_sku = models.CharField(unique=True, max_length=255)
    color = models.CharField(max_length=255)
    size = models.CharField(max_length=255)
    product = models.ForeignKey(Product, limit_choices_to=Q(group_sku__in=product_sku))
    ...

I'm importing my data to Django from an XML file via a python script.

group_sku comes in the format "GROUP123"

product_sku comes in the format "GROUP123-BLUE-M" (or similar) and there are multiple products in each group

How do I define the foreign key relationship so that each Product is linked to its respective ProductGroup? Or feel free to tell me if I'm going about this the complete wrong way.

Using Django 1.6.5, Python 2.7, and MySQL

Reply all
Reply to author
Forward
0 new messages