我是 django orm 的新手。 我的表格看起来像这样。
class Product(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4)
name = models.CharField(max_length=60)
class ProductOption(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4)
product_id = models.ForeignKey(Product, on_delete=models.CASCADE, null=True, blank=True)
我想查询与产品相关的productoption id。我做了这样的查询来进行内部联接。
Query = Product.select_related(‘product_id’).filter(name='a')
它给了我错误消息:
django.core.exceptions.FieldError: Invalid field name(s) given in select_related: 'product_id'. Choices are: (none)
我想知道模型或查询是否有问题。
最佳答案
Product.objects.filter(name='a').prefetch_related('productoption_set')
关于mysql - Django ORM : Tried to do inner join with foreign key but causes FieldError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47276990/