class Product(models.Model):
price=models.IntegerField()
no=models.IntegerField(default=0,null=True)
sku= models.CharField(max_length=100,default=0,null=True )
#Here I want to use sku into another model as a foreign key
class Product(models.Model):
price=models.IntegerField()
no=models.IntegerField(default=0,null=True)
sku= models.CharField(max_length=100,default=0,null=True )
#Here I want to use sku into another model as a foreign key
In that case, the sku needs to be unique, and non-nullable, since otherwise it can not refer to a product properly, so:
class Product(models.Model):
price = models.IntegerField()
no = models.IntegerField(default=0, null=True)
sku = models.CharField(max_length=100, unique=True)
Then you can work with the to_field=… parameter [Django-doc]:
class Order(models.Model):
product = models.ForeignKey(Product, to_field='sku', on_delete=models.CASCADE)
Then if you use my_order.product_id, it will contain the sku of the product it refers to.