Re: Add an optional parameter to values() that returns a nested dictionary for foreign keys

2015-11-25 Thread Moenad
I think I wasn't clear from the beginning, the idea of "nested" is to nest 
all possible levels, not just a single level. I like the idea of "N", given 
that you can have more control, but having something like N("person", 
"person__hometown", "person__hometown__country") which will be different 
than N("person__hometown__country") is confusing.

I have another idea, why not make the alias + nest possible with a single 
parameter, where it takes a dictionary and expect how the final structure 
and aliasing are?

For example:

{"id": "custom_id"
 "first_name": "custom_first_name",
 "last_name": "custom_last_name",
 "hometown": {
  "__alias__": "custom_hometown"
  "id": "custom_hometown_id",
  "name": "custom_name",
  "country": "custom_country"
  }
}

or to make it a standard in someway,

{"id": "custom_id"
 "first_name": "custom_first_name",
 "last_name": "custom_last_name",
 "hometown": {
  "__alias__": "custom_hometown"
  "hometown__id": "custom_hometown_id",
  "hometown__name": "custom_name",
  "hometown__country": "custom_country"
  }
}

as you noticed, if there's a foreign key for example, a new key "__alias__" 
or something should be added in the dict. Also, no need for values() *args, 
the dict structure would be more than enough?


On Wednesday, November 25, 2015 at 7:21:43 PM UTC+2, Joachim Jablon wrote:
>
> Marten's suggestion is quite interesting for providing a way to tell which 
> data you want nested and which data you don't. Plus, this form might be 
> interesting to solve another problem : how would Django know if we want :
>
> {"id": 1
>  "first_name": "first name",
>  "last_name": "last name",
>  "hometown": {
>   "id": 1,
>   "name": "town name",
>   "country": 3
>   }
> }
>
>
> # or
>
>
> {"id": 1
>  "first_name": "first name",
>  "last_name": "last name",
>  "hometown": {
>   "id": 1,
>   "name": "town name",
>   "country": {
> "id": 3,
> "name": "country name"
>   }
>   }
> }
>
>
>
> Limiting the nesting to a single level would be an arbitrary decision and 
> users should be able to control this (IMHO)
>
> So we could have a "level" argument that would say how many levels deep it 
> will search but then what if you want SOME nesting in some branches, not in 
> others, like : 
>
> {"id": 1
>  "first_name": "first name",
>  "last_name": "last name",
>  "hometown": {
>   "id": 1,
>   "name": "town name",
>   "country": {
> "id": 3,
> "name": "country name"
>   }
>   },
>   "father": 4
> }
>
>
> (here, "father" is another FK that we don't want expanded ?
>
> Maybe a syntax like :
>
> N("person", "person__hometown", "person__hometown__country")
> Note : this might not be equivalent to N("person__hometown__country"), 
> that you could use if you want ONLY the nested "country"
>
> I'd like that.
>
> And it's compatible with the suggestion of using **kwargs for aliasing 
> (for the top level element of the dict, at least)
>
> Le mercredi 25 novembre 2015 17:53:25 UTC+1, Marten Kenbeek a écrit :
>>
>> I think it'd be more consistent with other parts of the ORM to use 
>> **kwargs to specify aliases. For nested data you can use an object, say N, 
>> similar to Q and F objects:
>>
>> Articles.objects.filter(id=1).values('body', N('author'), my_custom_title
>> ='title')
>>
>> I'm no ORM expert, but could something like this be possible by allowing 
>> expressions in values() and using custom output fields?
>>
>> On Wednesday, November 25, 2015 at 5:09:29 PM UTC+1, Moenad wrote:
>>>
>>> Well, switch the field name aliasing to a dictionary without hijacking 
>>> **kwargs ?
>>>
>>> I prefer the following:
>>>
>>> Articles.objects.get(id=1).values(’title’, ’author’, ‘body', 
&

Re: Add an optional parameter to values() that returns a nested dictionary for foreign keys

2015-11-25 Thread Moenad
I like the idea but what about multiple nesting, multiple foreign keys?

We end up with something like N('author__book')? a bit confusing no?

On Wednesday, November 25, 2015 at 6:53:25 PM UTC+2, Marten Kenbeek wrote:
>
> I think it'd be more consistent with other parts of the ORM to use 
> **kwargs to specify aliases. For nested data you can use an object, say N, 
> similar to Q and F objects:
>
> Articles.objects.filter(id=1).values('body', N('author'), my_custom_title=
> 'title')
>
> I'm no ORM expert, but could something like this be possible by allowing 
> expressions in values() and using custom output fields?
>
> On Wednesday, November 25, 2015 at 5:09:29 PM UTC+1, Moenad wrote:
>>
>> Well, switch the field name aliasing to a dictionary without hijacking 
>> **kwargs ?
>>
>> I prefer the following:
>>
>> Articles.objects.get(id=1).values(’title’, ’author’, ‘body', 
>> alias={"title": "my_custom_title"}, nested=True)
>>
>>
>> On Wednesday, November 25, 2015 at 5:43:51 PM UTC+2, Tim Graham wrote:
>>>
>>> There's an accepted ticket for adding aliasing to values(): 
>>> https://code.djangoproject.com/ticket/16735
>>>
>>> The current patch there hijacks values() **kwargs for the mapping of 
>>> renamed fields which would prevent adding other kwargs like "nested" 
>>> without disallowing those values as aliases. I guess we may want to rethink 
>>> that approach.
>>>
>>> On Wednesday, November 25, 2015 at 10:20:37 AM UTC-5, Bobby Mozumder 
>>> wrote:
>>>>
>>>> I could also use a couple of enhancement to this:
>>>>
>>>> 1) Allow renaming of keys, instead of using the database column names.  
>>>> 2) Allow callbacks functions (or lambdas) to convert output values to 
>>>> another format if needed.
>>>>
>>>> With this, I could send the queries results right to JSON outputs.
>>>>
>>>> -bobby
>>>>
>>>> On Nov 25, 2015, at 9:05 AM, Moenad  wrote:
>>>>
>>>> Currently, after calling values() and the query executes, the output is 
>>>> a single level dictionary, including foreign keys. I propose adding an 
>>>> extra parameter for values, or at least values_list, where if it's set to 
>>>> true, a nested dictionary will be returned when there's a foreign key.
>>>>
>>>> Example:
>>>>
>>>> Person model with the following fields: first_name, last_name and 
>>>> hometown (foreign key)
>>>> Hometown model with the following fields: name
>>>>
>>>> A single record from Person.objects.values() will looks like this
>>>>
>>>> {"id": 1
>>>>  "first_name": "first name",
>>>>  "last_name": "last name",
>>>>  "hometown__id": 1,
>>>>  "hometown__name": "town name",
>>>> }
>>>>
>>>>
>>>> I propose adding a nested optional parameter to values, where a single 
>>>> record from Person.objects.values(nested=True) will look like
>>>>
>>>> {"id": 1
>>>>  "first_name": "first name",
>>>>  "last_name": "last name",
>>>>  "hometown": {
>>>>   "id": 1,
>>>>   "name": "town name"
>>>>   }
>>>> }
>>>>
>>>>
>>>> This feature is needed given that most APIs these days are nested, 
>>>> while it's simple to implement, I think it's much better to have it a 
>>>> built-in django feature.
>>>>
>>>>
>>>> -- 
>>>> You received this message because you are subscribed to the Google 
>>>> Groups "Django developers (Contributions to Django itself)" 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.
>>>> To view this discussion on the web visit 
>>>> https://groups.google.com/d/msgid/django-developers/8e5cbc9a-0317-40d3-8038-5b4300738b90%40googlegroups.com
>>>>  
>>>> <https://groups.google.com/d/msgid/django-developers/8e5cbc9a-0317-40d3-8038-5b4300738b90%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>> .
>>>> For more options, visit https://groups.google.com/d/optout.
>>>>
>>>>
>>>>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" 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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/1b790e4a-4767-443d-bc09-35f4ead19821%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Add an optional parameter to values() that returns a nested dictionary for foreign keys

2015-11-25 Thread Moenad
Well, switch the field name aliasing to a dictionary without hijacking 
**kwargs ?

I prefer the following:

Articles.objects.get(id=1).values(’title’, ’author’, ‘body', 
alias={"title": "my_custom_title"}, nested=True)


On Wednesday, November 25, 2015 at 5:43:51 PM UTC+2, Tim Graham wrote:
>
> There's an accepted ticket for adding aliasing to values(): 
> https://code.djangoproject.com/ticket/16735
>
> The current patch there hijacks values() **kwargs for the mapping of 
> renamed fields which would prevent adding other kwargs like "nested" 
> without disallowing those values as aliases. I guess we may want to rethink 
> that approach.
>
> On Wednesday, November 25, 2015 at 10:20:37 AM UTC-5, Bobby Mozumder wrote:
>>
>> I could also use a couple of enhancement to this:
>>
>> 1) Allow renaming of keys, instead of using the database column names.  
>> 2) Allow callbacks functions (or lambdas) to convert output values to 
>> another format if needed.
>>
>> With this, I could send the queries results right to JSON outputs.
>>
>> -bobby
>>
>> On Nov 25, 2015, at 9:05 AM, Moenad  wrote:
>>
>> Currently, after calling values() and the query executes, the output is a 
>> single level dictionary, including foreign keys. I propose adding an extra 
>> parameter for values, or at least values_list, where if it's set to true, a 
>> nested dictionary will be returned when there's a foreign key.
>>
>> Example:
>>
>> Person model with the following fields: first_name, last_name and 
>> hometown (foreign key)
>> Hometown model with the following fields: name
>>
>> A single record from Person.objects.values() will looks like this
>>
>> {"id": 1
>>  "first_name": "first name",
>>  "last_name": "last name",
>>  "hometown__id": 1,
>>  "hometown__name": "town name",
>> }
>>
>>
>> I propose adding a nested optional parameter to values, where a single 
>> record from Person.objects.values(nested=True) will look like
>>
>> {"id": 1
>>  "first_name": "first name",
>>  "last_name": "last name",
>>  "hometown": {
>>   "id": 1,
>>   "name": "town name"
>>   }
>> }
>>
>>
>> This feature is needed given that most APIs these days are nested, while 
>> it's simple to implement, I think it's much better to have it a built-in 
>> django feature.
>>
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Django developers (Contributions to Django itself)" 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.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/django-developers/8e5cbc9a-0317-40d3-8038-5b4300738b90%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/django-developers/8e5cbc9a-0317-40d3-8038-5b4300738b90%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" 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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/22edbb32-ff5c-4b1c-be9b-516d74014d4b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Add an optional parameter to values() that returns a nested dictionary for foreign keys

2015-11-25 Thread Moenad
100%, that would be great also. I thought of just posting the basic 
requirement that might be useful to most.

On Wednesday, November 25, 2015 at 5:20:37 PM UTC+2, Bobby Mozumder wrote:
>
> I could also use a couple of enhancement to this:
>
> 1) Allow renaming of keys, instead of using the database column names.  
> 2) Allow callbacks functions (or lambdas) to convert output values to 
> another format if needed.
>
> With this, I could send the queries results right to JSON outputs.
>
> -bobby
>
> On Nov 25, 2015, at 9:05 AM, Moenad > 
> wrote:
>
> Currently, after calling values() and the query executes, the output is a 
> single level dictionary, including foreign keys. I propose adding an extra 
> parameter for values, or at least values_list, where if it's set to true, a 
> nested dictionary will be returned when there's a foreign key.
>
> Example:
>
> Person model with the following fields: first_name, last_name and hometown 
> (foreign key)
> Hometown model with the following fields: name
>
> A single record from Person.objects.values() will looks like this
>
> {"id": 1
>  "first_name": "first name",
>  "last_name": "last name",
>  "hometown__id": 1,
>  "hometown__name": "town name",
> }
>
>
> I propose adding a nested optional parameter to values, where a single 
> record from Person.objects.values(nested=True) will look like
>
> {"id": 1
>  "first_name": "first name",
>  "last_name": "last name",
>  "hometown": {
>   "id": 1,
>   "name": "town name"
>   }
> }
>
>
> This feature is needed given that most APIs these days are nested, while 
> it's simple to implement, I think it's much better to have it a built-in 
> django feature.
>
>
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Django developers (Contributions to Django itself)" 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.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/django-developers/8e5cbc9a-0317-40d3-8038-5b4300738b90%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/django-developers/8e5cbc9a-0317-40d3-8038-5b4300738b90%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" 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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/628187c5-3c83-40d4-9aeb-c3ee1bc10119%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Add an optional parameter to values() that returns a nested dictionary for foreign keys

2015-11-25 Thread Moenad
Currently, after calling values() and the query executes, the output is a 
single level dictionary, including foreign keys. I propose adding an extra 
parameter for values, or at least values_list, where if it's set to true, a 
nested dictionary will be returned when there's a foreign key.

Example:

Person model with the following fields: first_name, last_name and hometown 
(foreign key)
Hometown model with the following fields: name

A single record from Person.objects.values() will looks like this

{"id": 1
 "first_name": "first name",
 "last_name": "last name",
 "hometown__id": 1,
 "hometown__name": "town name",
}


I propose adding a nested optional parameter to values, where a single 
record from Person.objects.values(nested=True) will look like

{"id": 1
 "first_name": "first name",
 "last_name": "last name",
 "hometown": {
  "id": 1,
  "name": "town name"
  }
}


This feature is needed given that most APIs these days are nested, while 
it's simple to implement, I think it's much better to have it a built-in 
django feature.

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" 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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/8e5cbc9a-0317-40d3-8038-5b4300738b90%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.