0
Je ces modèles Django:Filtre avec champ a une relation dans Django?
class Customer(models.Model):
def __unicode__(self):
return self.name
name = models.CharField(max_length=200)
class Sale(models.Model):
def __unicode__(self):
return "Sale %s (%i)" % (self.type, self.id)
customer = models.ForeignKey(Customer)
total = models.DecimalField(max_digits=15, decimal_places=3)
notes = models.TextField(blank=True, null=True)
class Unitary_Sale(models.Model):
book = models.ForeignKey(Book)
quantity = models.IntegerField()
unit_price = models.DecimalField(max_digits=15, decimal_places=3)
sale = models.ForeignKey(Sale)
Comment puis-je filtrer pour obtenir tous les livres que la vente à la clientèle?
J'ai essayé:
units=Unitary_Sale.objects.all()
>>> units=Unitary_Sale.objects.all()
>>> for unit in units:
... print unit.sale.customer
... print unit.book,unit.sale.total
...
Sok nara
Khmer Empire (H001) 38.4
Sok nara
killing field (H001) 16
San ta
khmer krom (H001) 20
San ta
Khmer Empire (H001) 20
>>>
ce que je veux:
sok nora:56.4 (38.4+18)
san ta:40 (20+20)
Ou dictionnaire:
{sok nora:156.4, san ta:40}
>>> store_resulte = {}
>>> for unit in units:
... store_resulte[unit.sale.customer] = unit.sale.total
...
>>> print store_resulte
{<Customer: Sok nara>: Decimal("16"), <Customer: san ta>: Decimal("20")}
Il devrait être:
{<Customer: Sok nara>: Decimal("56.4"), <Customer: san ta>: Decimal("40")}
AttributeError : L'objet 'Manager' n'a pas d'attribut 'annoter' – kn3l
Quelle version de Django utilisez-vous? essayez de remplacer 'annotate' par' aggregate'. – jbochi
C'est adapter ma question? Alors, que dois-je appliquer à mon quiz? :) – kn3l