Django QuerySet attribute as Float instead of Unicode - django

We are making a Pc builder compare website, so you can compare pc components prices and make a cheap pc. We are using external data with Scrapy that saves it into MongoDB, we use Django as framework.
I have a 'prijs' Array (price) of an object as unicode, this object is in the QuerySet. I want that each 'prijs' in that array is a Float instead of unicode. Is there some way to convert the unicode array to a normal float array. I Googled alot but no solution for me, I hope you can help me with our problem.
Sorry for my bad English, thanks in advance!
Here is one object of MongoDB:
> db.processoren.findOne()
{
"_id" : ObjectId("547db39af2125f612cb8a1e5"),
"info" : [
"Quad-core, Inclusief koeler"
],
"sku" : [
"BX80646I54590"
],
"prijs" : [
"188.00000000000000000000"
],
"categorie" : "processoren",
"herkomst" : "paradigit",
"naam" : [
"Intel Core i5-4590 - 3.3GHz - Socket 1150"
],
"link" : "http://www.paradigit.nl/intel-core-i5-4590-3-3ghz-socket-1150/80015736/details.aspx",
"stock" : [
"Op dit moment niet beschikbaar"
]
}
models.py
from django.db import models
from mongoengine import *
from APc.settings import DBNAME
connect(DBNAME)
class Processoren(Document):
herkomst = StringField(max_length=200)
categorie = StringField(max_length=120)
naam = StringField(max_length=500)
subnaam = StringField(max_length=500)
info = StringField(max_length=500)
prijs = FloatField()
stock = StringField(max_length=500)
ean = StringField(max_length=200)
sku = StringField(max_length=200)
herkomst = StringField(max_length=200)
link = StringField(max_length=200)
views.py
from django.shortcuts import render_to_response
from django.template.loader import render_to_string
from django.shortcuts import HttpResponseRedirect
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.template import RequestContext
from django.template import loader
from django.http import HttpResponse
from bson.json_util import dumps
from pcbuilder.compatibility import *
from pcbuilder.filters import *
import json as simplejson
from models import Processoren, Moederborden, Koeling, Behuizingen, Grafische, Harde, Dvd, Geheugen, Voeding
from itertools import chain
import json
def processoren(request):
processorenlijst = Processoren.objects
print type(processorenlijst[0].prijs[0])
This is the output of the print
<type 'unicode'>

You can easily convert a list of strings to floats by doing something like below.
unicode_list = [u'10', u'20', u'123.1']
[float(val) for val in unicode_list]
I've never used MongoDB but I know in db backends like MySQL and PSQL you enforce types. The ORM will then return the values as the correct types. Perhaps the real issue is the structure of your data.

Related

fastapi instrument with opentelemetry.propagators.cloud_trace_propagator

Just looking around to see if anyone has a working POC using the FastApi auto instruments along with GCP. Turns out that the fastApi Middleware makes a call to the CloudTraceFormatPropagator when we set
set_global_textmap(CloudTraceFormatPropagator())
however the carrier is not correct and returns to many values ( carrier in this case is a HTTPScope
ive tried the below, I need to be able to do distributed tracing which you can see within the foobar
import fastapi
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
import time
import requests
from starlette.middleware.base import BaseHTTPMiddleware
from typing import Callable, Tuple
from uuid import uuid4
from fastapi import Request
from opentelemetry import metrics, trace
from opentelemetry.exporter.cloud_monitoring import (
CloudMonitoringMetricsExporter,
)
from opentelemetry.exporter.cloud_trace import CloudTraceSpanExporter
from opentelemetry.propagate import set_global_textmap, inject, extract
from opentelemetry.propagators.cloud_trace_propagator import (
CloudTraceFormatPropagator,
)
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor,SimpleSpanProcessor
set_global_textmap(CloudTraceFormatPropagator())
resource = Resource.create(
{
"service.name": "fastapi_e2e_server",
"service.namespace": "examples",
"service.instance.id": "instance123",
}
)
tracer_provider = TracerProvider()
cloud_trace_exporter = CloudTraceSpanExporter()
tracer_provider.add_span_processor(
# BatchSpanProcessor buffers spans and sends them in batches in a
# background thread. The default parameters are sensible, but can be
# tweaked to optimize your performance
SimpleSpanProcessor(cloud_trace_exporter)
)
meter_provider = MeterProvider(
metric_readers=[
PeriodicExportingMetricReader(
CloudMonitoringMetricsExporter(), export_interval_millis=5000
)
],
resource=resource,
)
trace.set_tracer_provider(tracer_provider)
metrics.set_meter_provider(meter_provider)
tracer = trace.get_tracer(__name__)
meter = metrics.get_meter(__name__)
app = fastapi.FastAPI(title="whata")
def the_injection():
headers={}
inject(headers)
return headers
#app.get("/foobar")
def foobar(request: Request):
with tracer.start_as_current_span("Get Alerts Parent span") as parent:
span = trace.get_current_span()
span.add_event("endpoint Called", {
"url": request.url.path
})
with tracer.start_as_current_span("Get Alerts child span") as child:
head = the_injection()
res = requests.get("http://0.0.0.0:8082/target", headers=head)
return res.text
FastAPIInstrumentor.instrument_app(app)

use of upload_to function when creating on object in django

here is my model in django :
import os
def upload_location_buy(instance, filename):
ext = filename.split('.')[-1]
filename = '%s.%s' % (instance.name+"_"+str(random.randint(1000, 9999)), ext)
print(os.path.join('uploads', filename))
return os.path.join('uploads', filename)
class Buy(models.Model):
pic_front = models.ImageField(blank=True, upload_to=upload_location_buy,default='')
When i try :
b = Buy(pic_front="appartement_a.jpg")
b.save()
it does nothing exception linking the current pic in the current path, so not using the function in the model.
And when i do :
b = Buy(pic_front=upload_location_buy("appartement_a.jpg"))
b.save()
it give me an error because it seems to need the instance.
TypeError: upload_location_buy() missing 1 required positional
argument: 'filename'
How to create a buy object, giving it a picture and using the upload_location_buy whend doing it without the admin ?
When i upload a pic in the admin it works.
how to give it the instance name or do it correctly ?
this is this script :
import sys
import os
import django
from django.core.exceptions import ObjectDoesNotExist
sys.path.append("../../../testproject")
os.environ["DJANGO_SETTINGS_MODULE"] = "testproject.settings"
django.setup()
from on_plan.models import Buy
Buy.objects.all().delete()
b = Buy(pic_front=upload_location_buy("appartement_a.jpg"))
b.save()
The script is in the same directory as the file "appartement_a.jpg".
-on_plan/
-testproject/
-import_script.py
-appartement_a.jpg
-manage.py
if i do a :
file_to_save = open('appartement_a.jpg', 'r').read()
b = Buy(pic_front=file_to_save)
b.save()
i have a:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
if i do a :
file_to_save = open('appartement_a.jpg', 'rb').read()
b = Buy(pic_front=file_to_save)
b.save()
i have a:
AttributeError: 'bytes' object has no attribute '_committed'
the solution is here:
import requests
import urllib.parse
import sys
import os
import django
import csv
import time
import random
from django.core.exceptions import ObjectDoesNotExist
sys.path.append("../../../sample_estate")
os.environ["DJANGO_SETTINGS_MODULE"] = "sample_estate.settings"
django.setup()
from django.core.files.base import File
from on_plan.models import Buy
file_to_save = open('appartement_a.jpg', 'rb')
b = Buy(pic_front=File(file_to_save))
b.save()
When you create a Buy object you need to pass file-obejct into pic_front attribute, but you try to pass only name of file.
from django.core.files.base import File
file_to_save = open('/path/to/appartement_a.jpg', 'r').read()
b = Buy(pic_front=File(file_to_save))
b.save()
If you get file from a form you may upload it like that:
b = Buy(pic_front=request.FILES['file'])
b.save()
Anyway pic_front expect to getting a file-object not string
More about this case

django how to display a weeks data

Hi thanks for helping me
I being doing some browsing on google and stack overflow, but documentation django and python I can hardly understand, how the they make code run
I just can't figure out a way to display week data (table & chart) with
two toggle button to toggle different weeks of 53 weeks in a year
I had try using the week in Django template tags; https://docs.djangoproject.com/en/2.1/ref/templates/builtins/#date
but I get empty value instead; here example I did {{ value|date:"W" }}
is there easily way to do this? I do not wish to use the weekarchiveview:
https://docs.djangoproject.com/en/2.1/ref/class-based-views/generic-date-based/#weekarchiveview
As I need to toggle between years,months and weeks on same page.
Below are my codes
this my code for views
from django.shortcuts import render
from django.views.generic import ListView, DetailView ,TemplateView
from zigview.models import tank_system
from django.utils import timezone
from datetime import date, timedelta
class EC(ListView):
model = tank_system
template_name = 'FrounterWeb/extends/EC.html'
ordering = ['-datetime'] # sort dates in descending order
def get_context_data(self, **kwargs):
return {'tank': self.get_queryset()}
This is my apps url codes
from django.urls import path
from . import views #function views
from django.views.generic.base import TemplateView
from django.contrib.auth.decorators import login_required, permission_required
urlpatterns = [
path('',login_required(views.index.as_view()), name='index'), # views to call our index
path(r'', TemplateView.as_view(template_name='index.html'), name = 'myapp'), # tell django the which path to our main page
path(r'liveSterm/', login_required(views.Strem), name='Livesterm'), #call live strem page
path(r'EC-data/', login_required(views.EC.as_view()), name='EC'),
path(r'ph-data/', login_required(views.ph.as_view()), name='ph'),
path(r'Water-Temptures/', login_required(views.WT.as_view()), name='WT'),
path(r'Room-Temptures/', login_required(views.RT.as_view()), name= 'RT'),
path(r'Water-Flow-IN/', login_required(views.WaterFlowIN.as_view()), name= 'WFI'),
path(r'Water-Flow-OUT/', login_required(views.WaterFlowOUT.as_view()), name= 'WFO'),
]
this is my models codes
from django.db import models
from django.utils import timezone
from decimal import Decimal
# having errors KeyError: "'__name__' not in globals"
class tank_system(models.Model):
PH = models.DecimalField(max_digits=3, decimal_places=1)
EC = models.DecimalField(max_digits=3, decimal_places=1)
Winlet = models.DecimalField(max_digits=3, decimal_places=1)
Woutlet = models.DecimalField(max_digits=3, decimal_places=1)
WaterLevel = models.IntegerField(default=500)
TempWater = models.IntegerField(default=25)
TempRoom = models.IntegerField(default=25)
tanks = models.IntegerField(default=1)
datetime = models.DateTimeField(default=timezone.now())
You don't have 'value' in your context. The context is only 'tank'. So to get the datetime you can use {{ tank.datetime|date:"W" }}
To switch after clicking on a button you can write a simple piece of javascript that changes the inner html of a certain part of your DOM to {{ tank.datetime|date:"W" }} , {{ tank.datetime|date:"M" }} etc. after clicking the button

Django Model Field Callable Default Not Working

Django's callable model field default are broken. When I set a model field default equal to a callable (see migration file below), all models get the same value even though the callable returns different values each time it is called.
This worked on previous fields, so I'm confused why Django would be failing on this field. Everything works up until the point I migrate, when inspecting the database column reveals all values to be the same.
Migration file:
# -*- coding: utf-8 -*-
# Generated by Django 1.10.1 on 2018-05-10 14:53
from __future__ import unicode_literals
from django.db import migrations, models
import screen.models
class Migration(migrations.Migration):
dependencies = [
('screen', '0064_employer_enable_show_question_template_tags'),
]
operations = [
migrations.AddField(
model_name='question',
name='key',
field=models.TextField(default=screen.models.generate_question_key),
),
]
Model
def generate_question_key():
# return a random string 64 characters long
return generate_random_hash(64)
class Question(Model):
name = TextField()
points = PositiveIntegerField() # how much it's worth
time_spent = PositiveIntegerField(default = 0) # seconds
score = PositiveIntegerField(default = 0)
key = TextField(default = generate_question_key)
The default returns different values:
>>> import screen
>>> screen.models.generate_question_key()
'JpZzloZkiLyvPLrDZ9764VTWkNUon1FD08mGKODa2uiqW1nV422HXVvt78MsW7aR'
>>> screen.models.generate_question_key()
'NHyTwPDA2cAAsTeIR77INLMM6Ik14EQ6vTlrTv4ZwV56nt6jGEtR8bKn8iyWDeMA'
>>> screen.models.generate_question_key()
'q2aALA7WmvtiKLiGXfNEStpKhOFcNpMDrJ8Y9sv6mwWNsUU6mdgMlgaW5yJJ1yEI'
>>>
It looks like this is a limitation of Django: https://docs.djangoproject.com/en/1.11/howto/writing-migrations/#migrations-that-add-unique-fields

django-haystack 2.0.0beta facet example - not showing facet counts

I've been working through the documentation for Haystack 2.0.0beta, and using solr3.6.0 as my backend. I have gotten through the getting started example. Working with the facet example now.
search_indexes.py
import datetime
from haystack import indexes
from bsmain.models import Note
class NoteIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
author = indexes.CharField(model_attr='user', faceted=True)
pub_date = indexes.DateTimeField(model_attr='pub_date')
def get_model(self):
return Note
def index_queryset(self):
"""Used when the entire index for model is updated."""
return self.get_model().objects.filter(pub_date__lte=datetime.datetime.now())
urls.py
from django.conf.urls.defaults import *
from django.conf import settings
from django.conf.urls.defaults import *
from haystack.forms import FacetedSearchForm
from haystack.query import SearchQuerySet
from haystack.views import FacetedSearchView
sqs = SearchQuerySet().facet('author')
urlpatterns = patterns('haystack.views',
url(r'^$', FacetedSearchView(form_class=FacetedSearchForm,searchqueryset=sqs),
name='haystack_search'),
)
I have tested in python shell and get facets and counts, but when I fire the /search url (with the html provided in the example for facets) I get the form but no facets or counts.
Can anyone see anything wrong in the code above or is there somehing else I am missing?
Thanks.
Did you rebuild the index (./manage.py rebuild_index)?
Once done then create the schema.xml (./manage.py build_solr_scema > schema.xml)
Then copy the shcema.xml to your solr conf directory and restart the solr.
That should solve your problem.