tengo los siguientes modelos en django rest framework:
class Products(models.Model):
name_product = models.CharField(max_length=100, blank=False, null=False)
category = models.ForeignKey(Categories, on_delete=models.CASCADE, related_name='rel_category')
category_type = models.CharField(max_length=100, null=True)
class Meta:
verbose_name = 'Product'
verbose_name_plural = 'Products'
class Categories(models.Model):
id = models.AutoField(primary_key=True)
created_at = models.DateField('creation date', auto_now=True, auto_now_add=False)
name_category = models.CharField(max_length=100, blank=False, null=False)
class Meta:
verbose_name = 'Category'
verbose_name_plural = 'Categories'
def __str__(self):
return self.name_category
def __str__(self):
return self.name_product
class Stores(models.Model):
id_store = models.AutoField(primary_key=True)
created_at = models.DateField('creation date', auto_now=True, auto_now_add=False)
name_store = models.CharField(max_length=100, blank=False, null=False)
address = models.CharField(max_length=200, blank=False, null=False)
telephone = models.CharField(max_length=100, blank=False, null=False)
email = models.EmailField(max_length=254, blank=False, null=False)
number_identifier = models.CharField(max_length=100, blank=False, null=False)
manager = models.CharField(max_length=200, blank=False, null=False)
state_store = models.BooleanField('Estate', default=True)
city = models.ForeignKey(Cities, on_delete=models.CASCADE, null=True)
logo_store = models.CharField(max_length=200, blank=False, null=False)
description = models.TextField(max_length=500, null=True)
class Meta:
verbose_name = 'Store'
verbose_name_plural = 'Stores'
def __str__(self):
return self.name_store
En Sql para unir estos modelos seria de la siguiente manera:
select * from api_products pr
join api_stores st on st.id_store = pr.store_id
join api_categories cat on cat.id = pr.category_id;
Pero no consigo unirlos desde django con select_related no funciona por que los ForeignKey estan en el modelo contrario, el problema es que estoy trabajando con django rest entonces los serializers ponen problema agradeceria la ayuda.