Re: Hybrid Property request
On 17 July 2013 21:18, Jonathan Slenderswrote: > This is really nice. Both have their use cases, but I'm prefering the > second when possible, because it's much more flexible and preserves > database consistency. > With the same efford you can make the following work: > > ProductEntry.objects.filter(lambda p: p.onpurchase_price * p.quantity > == 5000.0) > > Or if you don't like the lambda keyword, extend the Q object. Something > like this: > > ProductEntry.objects.filter(Q(onpurchase_price) * Q(quantity) == 5000.0) > > ISTM it would be more of an extension of ExpressionNode, like F(). -- Curtis -- You received this message because you are subscribed to the Google Groups "Django developers" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+unsubscr...@googlegroups.com. To post to this group, send email to django-developers@googlegroups.com. Visit this group at http://groups.google.com/group/django-developers. For more options, visit https://groups.google.com/groups/opt_out.
Re: Hybrid Property request
Oh, and denormalisation is not necessarily faster. If you're able to express the value of the column as an SQL expression, it's possible to put an index on such an expression. Le mercredi 17 juillet 2013 13:18:16 UTC+2, Jonathan Slenders a écrit : > > This is really nice. Both have their use cases, but I'm prefering the > second when possible, because it's much more flexible and preserves > database consistency. > With the same efford you can make the following work: > > ProductEntry.objects.filter(lambda p: p.onpurchase_price * p.quantity > == 5000.0) > > Or if you don't like the lambda keyword, extend the Q object. Something > like this: > > ProductEntry.objects.filter(Q(onpurchase_price) * Q(quantity) == 5000.0) > > > Denormalisation is fine if you often do the same query. It's certainly > easier to implement, but I wouldn't encourage it too much. > > > > Le mercredi 17 juillet 2013 03:15:52 UTC+2, Alex_Gaynor a écrit : >> >> I'm going to suggest that a virtual field and a materialized field are >> slightly different concepts, and porblaly have different internals. The >> former requires some hooks in the ORM, the latter is really just some nice >> wrapping around existing hooks. >> >> In any event there's probably some overal with the composite fields GSOC >> for the virtual field, when we generalize fields to N>=1, I'm hoping that >> makes generlaizing them to N=0 easier. >> >> Alex >> >> >> On Wed, Jul 17, 2013 at 11:12 AM, Jacob Kaplan-Moss >>wrote: >> >>> On Tue, Jul 16, 2013 at 8:04 PM, Russell Keith-Magee < >>> rus...@keith-magee.com> wrote: >>> So - functionally, what you describe can be done. What's missing is someone to polish the concepts into a patch for inclusion into Django's repository. If this is something that interests you - get hacking! :-) >>> >>> I'll bet, in fact, that you could do *both* of these things in a >>> *single* implementation. A hybrid property (to use your term) with a >>> virtual/concrete flag sounds epic. Show me a good patch and I'll help get >>> it merged! >>> >>> :) >>> >>> Jacob >>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "Django developers" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to django-develop...@googlegroups.com. >>> To post to this group, send email to django-d...@googlegroups.com. >>> Visit this group at http://groups.google.com/group/django-developers. >>> For more options, visit https://groups.google.com/groups/opt_out. >>> >>> >>> >> >> >> >> -- >> "I disapprove of what you say, but I will defend to the death your right >> to say it." -- Evelyn Beatrice Hall (summarizing Voltaire) >> "The people's good is the highest law." -- Cicero >> GPG Key fingerprint: 125F 5C67 DFE9 4084 >> > -- You received this message because you are subscribed to the Google Groups "Django developers" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+unsubscr...@googlegroups.com. To post to this group, send email to django-developers@googlegroups.com. Visit this group at http://groups.google.com/group/django-developers. For more options, visit https://groups.google.com/groups/opt_out.
Re: Hybrid Property request
This is really nice. Both have their use cases, but I'm prefering the second when possible, because it's much more flexible and preserves database consistency. With the same efford you can make the following work: ProductEntry.objects.filter(lambda p: p.onpurchase_price * p.quantity == 5000.0) Or if you don't like the lambda keyword, extend the Q object. Something like this: ProductEntry.objects.filter(Q(onpurchase_price) * Q(quantity) == 5000.0) Denormalisation is fine if you often do the same query. It's certainly easier to implement, but I wouldn't encourage it too much. Le mercredi 17 juillet 2013 03:15:52 UTC+2, Alex_Gaynor a écrit : > > I'm going to suggest that a virtual field and a materialized field are > slightly different concepts, and porblaly have different internals. The > former requires some hooks in the ORM, the latter is really just some nice > wrapping around existing hooks. > > In any event there's probably some overal with the composite fields GSOC > for the virtual field, when we generalize fields to N>=1, I'm hoping that > makes generlaizing them to N=0 easier. > > Alex > > > On Wed, Jul 17, 2013 at 11:12 AM, Jacob Kaplan-Moss >> wrote: > >> On Tue, Jul 16, 2013 at 8:04 PM, Russell Keith-Magee < >> rus...@keith-magee.com > wrote: >> >>> So - functionally, what you describe can be done. What's missing is >>> someone to polish the concepts into a patch for inclusion into Django's >>> repository. If this is something that interests you - get hacking! :-) >>> >> >> I'll bet, in fact, that you could do *both* of these things in a *single* >> implementation. A hybrid property (to use your term) with a >> virtual/concrete flag sounds epic. Show me a good patch and I'll help get >> it merged! >> >> :) >> >> Jacob >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Django developers" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to django-develop...@googlegroups.com . >> To post to this group, send email to >> django-d...@googlegroups.com >> . >> Visit this group at http://groups.google.com/group/django-developers. >> For more options, visit https://groups.google.com/groups/opt_out. >> >> >> > > > > -- > "I disapprove of what you say, but I will defend to the death your right > to say it." -- Evelyn Beatrice Hall (summarizing Voltaire) > "The people's good is the highest law." -- Cicero > GPG Key fingerprint: 125F 5C67 DFE9 4084 > -- You received this message because you are subscribed to the Google Groups "Django developers" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+unsubscr...@googlegroups.com. To post to this group, send email to django-developers@googlegroups.com. Visit this group at http://groups.google.com/group/django-developers. For more options, visit https://groups.google.com/groups/opt_out.
Re: Hybrid Property request
I'm going to suggest that a virtual field and a materialized field are slightly different concepts, and porblaly have different internals. The former requires some hooks in the ORM, the latter is really just some nice wrapping around existing hooks. In any event there's probably some overal with the composite fields GSOC for the virtual field, when we generalize fields to N>=1, I'm hoping that makes generlaizing them to N=0 easier. Alex On Wed, Jul 17, 2013 at 11:12 AM, Jacob Kaplan-Mosswrote: > On Tue, Jul 16, 2013 at 8:04 PM, Russell Keith-Magee < > russ...@keith-magee.com> wrote: > >> So - functionally, what you describe can be done. What's missing is >> someone to polish the concepts into a patch for inclusion into Django's >> repository. If this is something that interests you - get hacking! :-) >> > > I'll bet, in fact, that you could do *both* of these things in a *single* > implementation. A hybrid property (to use your term) with a > virtual/concrete flag sounds epic. Show me a good patch and I'll help get > it merged! > > :) > > Jacob > > -- > You received this message because you are subscribed to the Google Groups > "Django developers" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to django-developers+unsubscr...@googlegroups.com. > To post to this group, send email to django-developers@googlegroups.com. > Visit this group at http://groups.google.com/group/django-developers. > For more options, visit https://groups.google.com/groups/opt_out. > > > -- "I disapprove of what you say, but I will defend to the death your right to say it." -- Evelyn Beatrice Hall (summarizing Voltaire) "The people's good is the highest law." -- Cicero GPG Key fingerprint: 125F 5C67 DFE9 4084 -- You received this message because you are subscribed to the Google Groups "Django developers" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+unsubscr...@googlegroups.com. To post to this group, send email to django-developers@googlegroups.com. Visit this group at http://groups.google.com/group/django-developers. For more options, visit https://groups.google.com/groups/opt_out.
Re: Hybrid Property request
On Tue, Jul 16, 2013 at 8:04 PM, Russell Keith-Magee < russ...@keith-magee.com> wrote: > So - functionally, what you describe can be done. What's missing is > someone to polish the concepts into a patch for inclusion into Django's > repository. If this is something that interests you - get hacking! :-) > I'll bet, in fact, that you could do *both* of these things in a *single* implementation. A hybrid property (to use your term) with a virtual/concrete flag sounds epic. Show me a good patch and I'll help get it merged! :) Jacob -- You received this message because you are subscribed to the Google Groups "Django developers" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+unsubscr...@googlegroups.com. To post to this group, send email to django-developers@googlegroups.com. Visit this group at http://groups.google.com/group/django-developers. For more options, visit https://groups.google.com/groups/opt_out.
Re: Hybrid Property request
On Wed, Jul 17, 2013 at 4:02 AM, Luis Francisco Jesus Masuelli Bonanni < endless.l...@gmail.com> wrote: > A feature I would really appreciate (as devs in > http://stackoverflow.com/questions/12217763/does-django-orm-have-an-equivalent-to-sqlalchemys-hybrid-attribute, > at least) is the Hybrid Property feature present in SQLAlchemy. They would > really contribute to DRY principle. > > In django this would allow using it as a left-side for the key=value in a > filter query (in sqlalchemy they filter by . as part of an > expression ). Example: > > class Purchase(models.Model): > first_name = CharField(...) > last_name = CharField(...) > ... > > @hybrid_property > def full_name(self): > return self.first_name + ' ' + self.full_name > > or ... > > class ProductEntry(models.Model): > purchase = ForeignKey(Purchase, related_name='product_entries') > onpurchase_price = DecimalField(...) > product = ForeignKey(Product) > quantity = DecimalField(...) > ... > > @hybrid_property > def cost(self): > return self.onpurchase_price * self.quantity > > > ... being those queries valid and working as expected > > Person.objects.filter(full_name__ilike='Martin') > ProductEntry.objects.filter(cost__lt=5000.0) #SQLAlchemy would use > ProductEntry.cost < 5000.0 > To dust off an old trope that I haven't used in a while…. … and a pony! Seriously -- what you've described sounds interesting, and the idea isn't a new one. Depending on exactly what you're intending, you could be describing: * A denormalized property. That is, an extra column is stored in the database with the computed value. * A virtual property - that is, there's no extra column, but you can use the property in queries, and it will be rolled out as a computed alias column in queries. Both of these have been proposed in the past (ticket #8946 for denormalized properties, for example). In the meantime, both of these can be done with the ORM as it stands. There are a couple of denormalized property implementations out in the wild; failing that, you can handle it with a normal Django field, populated in the model's save() method. Virtual properties can be emulated using an extra() clause in your query. It's not as elegant as the syntax you describe, but it works. So - functionally, what you describe can be done. What's missing is someone to polish the concepts into a patch for inclusion into Django's repository. If this is something that interests you - get hacking! :-) Yours, Russ Magee %-) -- You received this message because you are subscribed to the Google Groups "Django developers" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+unsubscr...@googlegroups.com. To post to this group, send email to django-developers@googlegroups.com. Visit this group at http://groups.google.com/group/django-developers. For more options, visit https://groups.google.com/groups/opt_out.
Hybrid Property request
A feature I would really appreciate (as devs in http://stackoverflow.com/questions/12217763/does-django-orm-have-an-equivalent-to-sqlalchemys-hybrid-attribute, at least) is the Hybrid Property feature present in SQLAlchemy. They would really contribute to DRY principle. In django this would allow using it as a left-side for the key=value in a filter query (in sqlalchemy they filter by . as part of an expression ). Example: class Purchase(models.Model): first_name = CharField(...) last_name = CharField(...) ... @hybrid_property def full_name(self): return self.first_name + ' ' + self.full_name or ... class ProductEntry(models.Model): purchase = ForeignKey(Purchase, related_name='product_entries') onpurchase_price = DecimalField(...) product = ForeignKey(Product) quantity = DecimalField(...) ... @hybrid_property def cost(self): return self.onpurchase_price * self.quantity ... being those queries valid and working as expected Person.objects.filter(full_name__ilike='Martin') ProductEntry.objects.filter(cost__lt=5000.0) #SQLAlchemy would use ProductEntry.cost < 5000.0 -- You received this message because you are subscribed to the Google Groups "Django developers" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+unsubscr...@googlegroups.com. To post to this group, send email to django-developers@googlegroups.com. Visit this group at http://groups.google.com/group/django-developers. For more options, visit https://groups.google.com/groups/opt_out.