class GoodsType(models.Model): name = models.CharField(max_length=50) def __unicode__(self): return self.name class Meta: ordering = ('id',)
class SubGoodsType(models.Model): type = models.ForeignKey(GoodsType, unique=False) name = models.CharField(max_length=50) def __unicode__(self): return self.name class Meta: ordering = ('type',)
class GoodsDescription(models.Model): subtype = models.ForeignKey(SubGoodsType, unique=False) basket = models.ManyToManyField(User, unique=False, null=True, blank=True) title = models.CharField(max_length=50, unique=False) articul = models.CharField(max_length=15, unique=False) image = models.ImageField(upload_to="description") description = models.TextField(max_length=4000) characteristics = models.TextField(max_length=4000, unique=False, null=True, blank=True) review = models.TextField(max_length=4000, unique=False, null=True, blank=True) number = models.IntegerField() price = models.CharField(max_length=15, unique=False, null=True, blank=True)
Так же есть функция представления:
def GoodsDescriptionCatalogPrint (request, self): subsubgoods = GoodsDescription.objects.filter(subtype=self) subgoods = SubGoodsType.objects.filter(id=self) goods = GoodsType.objects.get(id=subgoods.type) return render_to_response ("goods/catalog.html", { 'subsubgoods': subsubgoods, 'id_subgoods': self, 'goods': goods, }, context_instance = RequestContext(request))
Задача получить список объектов GoodsDescription которые принадлежат к определенному SubGoodsType, получить 1 объект SubGoodsType, что бы выбрать из него name и type, а за тем получить один объект GoodsType, что бы выбрать его name.
Я не могу правильно составить запросы, потому, что SubGoodsType может содержать множество одинаковых GoodsType.id и get возвращает ошибку. А если оставляю представление как есть сейчас сервер выдает ошибку, потому как получает множество объектов насколько я понимаю: ‘QuerySet’ object has no attribute ‘type’.
Как исправить запросы?