I have a Style table to identify a model number for each product type:
class Style(models.Model):
style = models.AutoField(primary_key=True)
name = models.CharField('Title', max_length=150)
etc..
And I need a product table which identifies all the product variations of the aforemention model number:
class Product(models.Model):
product = models.AutoField(primary_key=True)
style = models.ForeignKey(Style, db_column='style')
color = models.CharField(max_length=250, blank=True)
price = models.IntegerField(max_length=10, blank=True)
etc..
However, the client wants to be able to suggest other products that go with the aforementioned style. So I wanted to add a few "related product" columns as a Foreign Key. But neither MySQL nor Django lets me add them.
So for now I just made them integer fields. I added below lines to the Style model:
related_1 = models.IntegerField('Related Product1',blank=True,max_length=10, default=0)
related_2 etc..
So for example, a polo shirt has a styleId =1.
It has 4 different colors and 3 fabrics.
7 products in all for 1 style.
The product table will have seven rows all with a styleid=1.
The vendor will manually select products that are related to the style, which go into related_1, related_2 etc.
I understand Django and MySQL views some potential circular logic there, because each product has a FK to a styleId. So if that styleId has a foreign key back to the same product, clashes can occur.
But in fact, you obviously don't want to allow the related products to reference a product w/ the same styleId...which can easily be handled by the app rather than the database.
My workaround allows the user to manually enter the related products into the form, but it would be really cool if I could get Django to pull a select dropdown with a list of valid product Ids. If possible.
Is it?
P.S. I did read the django docs on related_field but couldn't really follow what they were getting at. I tried a few things, but they didn't provide much in the way of sample code.
2nd P.S. I edited it per request w/ more detailed model info. I hope it's clearer now...