How to parse serializer data in django rest framework - django

enter image description here
I want to parse this so that i can get a list of phase id to run another query for specific reason. this is serializer.data Response.
how to parse this to get a list of ids ?

Let's say the array from the image is stored in myArray:
result = [item["phase_id"] for item in myArray]

Related

All values for Batch Geocode outCols

There´s a way to receive all output fields using Batch Geocode endpoint?
I´m using this endpoint but I would like get all outCols fields available:
https://batch.geocoder.ls.hereapi.com/6.2/jobs?apiKey={MY_KEY}&action=run&inDelim=;&outDelim=;&outCols={WAY_TO_GET_ALL_FIELDS}&language=pt-BR&gen=8&header=true&outputCombined=true
outcols Enumeration, Required List of columns to return in the output.
So in outcols, we will have to provide the list of columns to return in the output.
Refer more details : https://developer.here.com/documentation/batch-geocoder/dev_guide/topics/request-parameters.html
Also , there are hundreds of out put fields but in customer use case all are not required at once.
details of all output fields : https://developer.here.com/documentation/batch-geocoder/dev_guide/topics/read-batch-request-output.html

Shopify API to get All Records for Customers,Orders & Products (Django)

I have searched for getting all customer one by one.But after some study understand the whole way to solve.
for getting 250 data from shopify api we can use limit but pagination and synchronization for getting all data we need some step to get all data.
shop_url = "https://%s:%s#%s.myshopify.com/admin/api/%s/" % (API_KEY, PASSWORD, SHOP_NAME, API_VERSION)
endpoint = 'customers.json?limit=250&fields=id,email&since_id=0'
r = requests.get(shop_url + endpoint)
Step 1:Where we need to put the initial id to start extraction and store to your db
customers.json??limit=250&fields=id,email&since_id=0
Step 2:Next changes the since_id value with with last id of your extraction like my image.
last id=5103249850543 (suppose)
Mentioned in Fields Data
customers.json??limit=250&fields=COLUMN_YOUNEED_FOR_CHK&since_id=5103249850543

How to skip some rows that have errors in django-excel using save_to_database()

I'm usign django-excel library in my Django project, and I want to skip some rows before save it to the database using the save_to_database() method.
I have something like the following:
file = form.cleaned_data['file']
file.save_to_database(
model=MyModel,
mapdict = fields,
initializer=self.choice_func,
)
All is working ok but I want to validate the data before call save_to_database function. The idea to do it is to add the rows that are not valid in an array and return it to notify the user that those fields not were saved.
Finally I achieve this goal returning None instead of the row in self.choice_fun function:
This function look like the following:
def choice_fun(self,row):
# Do whatever thing to validate your row
if row[5] != SOME_VALUE:
return None
return row
I also used some global variables to checkout if some of the rows had some error. Then I returned back that data to the response to give the user a feedback in some way.

How to remove the name "Queryset" from queryset data that has been retrieved in Django Database?

we all know that if we need to retrieve data from the database the data will back as a queryset but the question is How can I retrieve the data from database which is the name of it is queryset but remove that name from it.
maybe I can't be clarified enough in explanation so you can look at the next example to understand what I mean:
AnyObjects.objects.all().values()
this line will back the data like so:
<QuerySet [{'key': 'value'}]
now you can see the first name that is on the left side of retrieving data which is: "QuerySet" so, I need to remove that name to make the data as follows:
[{'key': 'value'}]
if you wonder about why so, the abbreviation of answer is I want to use Dataframe by pandas so, to put the data in Dataframe method I should use that layout.
any help please!!
You don't have to change it from a Queryset to anything else; pandas.DataFrame can take any Iterable as data. So
df = pandas.DataFrame(djangoapp.models.Model.objects.all().values())
Gives you the DataFrame you expect. (though you may want to double check df.dtypes. If there are Nones in your data, the column may end up to be of object type.)
You can use list(…) to convert it to a list of dictionaries:
list(AnyObjects.objects.values())
You will need to serialize it with the json package to obtain a JSON blob, since strings with single quotes are not valid JSON, in order to make it a JSON blob, you can work with:
import json
json.dumps(list(AnyObjects.object.values()))

Django append to JSON after serializers.serialze has been run on a queryset

I am returning a JSON serialized queryset using the following queryset:
genome_parents = Genome.objects.filter(genes=cus_id)
where cus_id is the FK pointing to a companies table so I am retrieving all Genome objects related to the current working company. I return this data after a form has been posted via:
genome_parents = serializers.serialize('json', genome_parents, use_natural_keys=True)
However, I need the natural key for one of my foreign keys, but the id for another (both on the same model). So one field is displayed nicely, but the other isn't. So this does what I need except for one little thing, I need the plain id number so that I can pre-populate my FK form field.
One thought I had was to just append something like
genome_parents.append({'id':gene.id})
but that obviously doesn't work. Is there anyway I can augment the JSON so that I can include one more little piece of data (or change how I format the JSON)?
Greg
Just switch the order of the operations. And put the entire gene object into the list so it is properly serialized.
genome_parents = list( Genome.objects.filter(genes=cus_id) )
genome_parents.append(gene)
json_genome_parents = serializers.serialize('json', genome_parents, use_natural_keys=True)