Django QuerySet Regex with MongoDB - django

I am trying to use Django QuerySet/Filter with Regular Expression to filter the records in my MongoDB database. For the Python packages, I'm using:
Django 4.0.3
djongo 1.3.6
pymongo 3.12.6
Here's my current attempt (code):
import re
from .models import User
regex = re.compile(pattern)
result = User.objects.filter(name__iregex=regex.pattern)
However, I get djongo.exceptions.SQLDecodeError every time I use the filter like above.
After looking through StackOverflow, I find out that I can filter by using Django raw but I still want to know if there are any other ways to make the above codes viable.

Related

Database error in django+mongodb objects.fiter with boolean field

I am using djongo library to handle mongodb, but still getting database error while filtering queryset using a boolean field.
Error: No exception message supplied,
django.db.utils.DatabaseError
from django.contrib.auth import get_user_model
User = get_user_model()
users = User.objects.filter(isVerified=True)
first of all django is a backend framework not mongodb libraray
secound of all you are using django orm which doesn't support noSQL databases
right now django orm support only relational dbms
for handle mongodb PyMongo is a good library

Django: Is it possible to download a csv file from another website and add it to FileField through Django shell?

While using Django, when I need to do something on data from DB, I've always been using Django shell. For this time, I want to do something like the below scenario in Django shell.
I have a model Store with a bunch of stores. In Django shell,
1. import Store model
2. With each of store name, I search each name and download a csv file based on each store name.
3. Add the downloaded csv file to a FileField in Store model.
I know how to #1 and #3, but I'm confused how I can do #2. How can I input the store name and download a csv file from the store name in Django shell?
You can use the requests library to fetch data from the internet. Install it like this:
pip install requests
Then, in Django, you can use ContentFile to save the downloaded data into a FileField. Here's an example:
import requests
from django.core.files.base import ContentFile
from .models import SomeModel
response = requests.get('https://raw.githubusercontent.com/wireservice/csvkit/master/examples/test_geo.csv')
s = SomeModal.objects.get(id=0)
s.file_field.save('data.csv', ContentFile(response.content))
s.save()

How to save inputs from Django forms?

I have a very entry level question but I could figure it out... My question is how to save inputs created by Django forms to a database (like google datastore)? It seems like there is no .save or .put functions associated with forms. Can anyone give me some suggestions or point out some examples?
Thanks!
Here is the Django forms and the website is hosted by Google app engine.
import os
os.environ['DJANGO_SETTINGS_MODULE']='settings'
from django import forms
from django.utils.safestring import mark_safe
class KabamInp(forms.Form):
chemical_name = forms.CharField(widget=forms.Textarea (attrs={'cols': 20, 'rows': 2}))
Koc = forms.FloatField(label=mark_safe('K<sub>OC</sub> (mL/g OC)'),initial=25000)
you need to create a Model class for the Form and use the values you get from the form to create your entity.
Look into the sample of Google App Engine Djnago Guest Book
It will guide thru creating a model/view/controller (MVC) application using Django on the App Engine.

Differences between Django on AppEngine and Django on my Linux Server?

I am a Django beginner, and I want to make this tutorial as exercise: http://www.joeyb.org/blog/2009/05/28/django-based-blog-on-google-app-engine-tutorial-part-1
The thing is that this tutorial is for AppEngine, but I want to do the tutorial in my Linux Development machine using a common database.
I have noticed that there are few differences:
In the Models:
from appengine_django.models import BaseModel
from google.appengine.ext import db
class BlogPost(BaseModel):
title = db.StringProperty()
uri = db.StringProperty()
date = db.DateTimeProperty(auto_now_add=True)
teaser = db.TextProperty()
teaser_html = db.TextProperty()
content = db.TextProperty()
content_html = db.TextProperty()
tags = db.StringProperty()
These imports are different:
from appengine_django.models import BaseModel
from google.appengine.ext import db
If I change this by:
from django.db import models
It will work?
Then I noticed one more reference to AppEngine:
from google.appengine.api import users
from google.appengine.ext.db import djangoforms
What imports should I use here to make this compatible with my Django on my Linux development server?
Best Regards,
AppEngine is not Django. There are ways of getting Django to work (more or less) on AppEngine, but that tutorial is specifically for AppEngine, not Django.
If you want to learn Django, do a Django tutorial. There are enough out there on the web.

How to map PostgreSQL array field in Django ORM

I have an array field in my PostrgreSQL database of type text. Is there a way to map this into a Django model ?
You might want to look into django-dbarray on github. It adds support for postgresql array fields.
I haven't used it before, but it looks like you just need to do:
from django.db import model
import dbarray
class ProfilingTestRun(models.Model):
function = models.CharField(max_length=64)
runtimes = dbarray.FloatArrayField()
One of the other nice options is http://django-orm.readthedocs.org/ --- a library that adds bindings to many native postgres types.
Main drawback of django-orm is that as of today it has no working support for south.
djorm-ext-pgarray also offer queries
http://www.niwi.be/2012/10/07/postgresql-array-fields-with-django/
Native support for PostgreSQL specific model fields is coming soon to Django (in the django.contrib.postgres.fields module):
https://docs.djangoproject.com/en/dev/ref/contrib/postgres/fields/#arrayfield
https://github.com/django/django/pull/2485 : The corresponding pull request
Since Django 1.8 there is a django.contrib.postgress module that adds support to array fields among other PostgreSQL data types.
For example you can do something like this:
from django.contrib.postgres.fields import ArrayField
from django.db import models
class GoGame(models.Model):
board = ArrayField(ArrayField(models.IntegerField(),size=19),size=19)
you have to subclass model.Field and write input and output methods.
http://docs.djangoproject.com/en/dev/howto/custom-model-fields/#custom-database-types