Re: Creating Objects from Annotations and Other Models

2019-03-21 Thread Dylan Young
Thanks Simon. That's exactly what I'm looking for. Wish they had piggy backed in the established F pattern though. It's quite verbose just to access the field on either model. r_id would just be a pk, usually from a fk on another model that I do need to fetch. Best, Casey Le jeu. 21 mars

Re: Creating Objects from Annotations and Other Models

2019-03-21 Thread Dylan Young
It's perfectly clear and precisely what an ORM is for. If you don't have an answer, just say so, though I suspect the answer is use SubQuery. It is not "useless" in general (obviously if you want to display/ deliver to an end user, you need to pluck from the database). It is useless in the

Re: Creating Objects from Annotations and Other Models

2019-03-21 Thread Simon Charette
I'm not sure where r_id comes from but Model.save usually deals with any expression correctly so you could try using a subquery assignment and see if it works. e.g. entry = Entry.objects.create( start=Subquery( Recurrence.objects.filter(pk=r_id).values('start_time')[:1] ) )

Re: Creating Objects from Annotations and Other Models

2019-03-21 Thread Chasan KIOUTSOUK MOUSTAFA
You can override the save method or simply use Django signals to modify fields of Model automatically. On Thu, Mar 21, 2019 at 8:21 PM Derek wrote: > Your use case is not very clear. > > If you are simply "running a few trivial

Re: Creating Objects from Annotations and Other Models

2019-03-21 Thread Derek
Your use case is not very clear. If you are simply "running a few trivial calculations and storing the info" then you could use a function in the database to do this for you - pulling data directly from Table A and storing the result in Table B. Data in Table B could then be later edited using

Re: Creating Objects from Annotations and Other Models

2019-03-21 Thread Dylan Young
For example using made-up syntax: Entry.objects.create(start=F(Recurrence.objects.filter(pk=r_id), 'start_time')) Note that we can't use `get` since the whole point is to avoid all the useless round-trips to the DB. Best, Casey On Thu, 21 Mar 2019 at 13:18, Dylan Young wrote: > It's not a

Re: Creating Objects from Annotations and Other Models

2019-03-21 Thread Dylan Young
It's not a recalculation. It's instantiation. Instance data diverges from the factory data and needs to be independently editable. Best, Casey Le jeu. 21 mars 2019 2:52 a.m., Derek a écrit : > Permanently storing data that can be recalculated from existing data is a > practice specific to

Re: Creating Objects from Annotations and Other Models

2019-03-20 Thread Derek
Permanently storing data that can be recalculated from existing data is a practice specific to the needs of particular applications. Have you perhaps considered making use of the "view" table functionality available in most databases? For example, in PostgreSQL you can create a materialized

Creating Objects from Annotations and Other Models

2019-03-19 Thread Dylan Young
Hey all: I often find myself fetching data from models in the database only to run a few trivial calculations and store the info in a different model. Is there an established ORM pattern for directly creating objects in the DB based on the fields of other models? Sorry if this is obvious, but