On Monday 20 February 2017 17:09:40 eltonplima wrote:
> Base class is abstract.
>
> class Base(models.Model):
> plano = models.ForeignKey(Plano)
>
>
> class Meta:
> abstract = True
>
>
> base_manager_name
> <https://docs.djangoproject.com/en/1.10/ref/models/options/#base-manag
> er-name>
Ack, my brain kept reading "default_manager_name".
Still - without a ForeignKey this shouldn't break anything. It seems it
warrents a new
bug report, but I'm curious *where* things break. It looks like this model is
used as an
inline in the admin. Is that correct?
I cannot reproduce this with a simple test case:
models:
*class CartEntryManager(*models.Manager*): def get_queryset(*self*):
*qs *=
/super/(*CartEntryManager, self*)*.get_queryset*() *expression *=
*models.F*('quantity') * *models.F*('price') *wrapped *=
*models.ExpressionWrapper*(*expression,
output_field*=*models.DecimalField*(
*max_digits*=*20,
decimal_places*=*2*)) return *qs.annotate*(*amount*=*wrapped*)
class Cart(*models.Model*): *created *=
*models.DateTimeField*(*auto_now_add*=True)
class CartEntry(*models.Model*): *item *=
*models.CharField*(*max_length*=*32,
primary_key*=True) *quantity *= *models.PositiveSmallIntegerField*()
*price *=
*models.DecimalField*(*max_digits*=*20, decimal_places*=*2*) *cart *=
*models.ForeignKey*(*Cart, on_delete*=*models.CASCADE,
related_name*='items') *is_shipped *= *models.BooleanField*(*default*=False)
*objects *= *CartEntryManager*()
class Meta: *base_manager_name *= 'objects'
*
tests:
*class CartTest(*TestCase*): /@/classmethod def setUpTestData(*cls*):
*cart
*= *models.Cart*() *cart.save*() *keyboard *= *models.CartEntry*(
*item*='Komplete Kontrol S88'*, quantity*=*2,
price*='999.00'*,
cart*=*cart *) *mixer *= *models.CartEntry*(
*item*='Traktor Kontrol
S8'*, quantity*=*1, price*='1199.00'*,
cart*=*cart *)
*keyboard.save*() *mixer.save*() *cls.cart *= *cart
cls.keyboard *=
*keyboard cls.mixer *= *mixer
*def test_entry_annotation(*self*):
*self.assertEqual*(*self.keyboard.amount,
1998*)
def test_base_manager(*self*): from *decimal *import *Decimal
total *=
*Decimal*('0.00') for *item *in *self.cart.items.all*():
*total *+=
*item.amount
total *= *total.quantize*(*Decimal*('0.01')) *expect *=
*Decimal*('3197.00')*.quantize*(*Decimal*('0.01'))
*self.assertEqual*(*total,
expect*)
def test_base_manager_update(*self*):
*self.cart.items.update*(*is_shipped*=True) *shipped *=
*self.cart.items.filter*(*is_shipped*=True)*.count*()
*self.assertEqual*(*shipped,
2*)
*
> On Saturday, February 18, 2017 at 9:20:03 PM UTC-3, Melvyn Sopacua wrote:
> > On Saturday 18 February 2017 22:27:46 Elton Pereira wrote:
> > > class ProdutoServicoManager(models.Manager):
> > >
> > > def get_queryset(self):
> > >
> > > expression = models.Sum(models.F('custounitario__valor') *
> > >
> > > models.F('custounitario__quantidade'))
> > >
> > > total_expr = models.ExpressionWrapper(expression,
> > >
> > > output_field=models.DecimalField())
> > >
> > > return
> > >
> > > super().get_queryset().annotate(custo_producao=total_expr)
> >
> > > class ProdutoServico(Base):
> > What's the manager on Base?
> >
> > > produto = models.BooleanField(default=True)
> > >
> > > descricao = models.TextField()
> > >
> > > objects = ProdutoServicoManager()
> > >
> > >
> > >
> > > class Meta:
> > >
> > > base_manager_name = 'objects'
> >
> > Cause this doesn't actually do anything, beside being explicit.
> >
> >
> >
> >
> > Melvyn Sopacua
--
Melvyn Sopacua
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/7674223.eRDJ6MCrYX%40devstation.
For more options, visit https://groups.google.com/d/optout.