Using Flask's MongoEngine. Error with datetime field - python

I have a model with the a datetime field
user_since = db.DateTimeField()
When I try to insert a new object of the model into mongo, There is no error. But the write does not succeed.
I printed the object from to_json() and tried to insert it with mongo shell, I get the following error.
field names cannot start with $ [$date] at src/mongo/shell/collection.js:L147
the to_json had this field.
"user_since": {"$date": 1392205572989}
I can't seem to find any pointers on how to solve this.
What is causing the write to fail?
How can I make mongoengine to throw error in case of write failure.? Or at least find out what the error was?
Thanks.
Update:
As I found later the real problem is not the datetime field. The details of the problem are in this question MongoEngine Document Object made using from_json doesn't save

With MongoDB you cannot have '$' at the beginning of the field name.
MongoDB Limits and Thresholds - Restrictions on Field Names:
Field names cannot contain dots (i.e. .), dollar signs (i.e. $), or
null characters. See Dollar Sign Operator Escaping for an alternate
approach.
Try to name it differently.
edit:
According to MongoEngine documentation, you could pass db_name parameter:
db_field – The database field to store this field in (defaults to the
name of the field)

Related

How do you incrementally add lexeme/s to an existing Django SearchVectorField document value through the ORM?

You can add to an existing Postgresql tsvector value using ||, for example:
UPDATE acme_table
SET my_tsvector = my_tsvector ||
to_tsvector('english', 'some new words to add to existing ones')
WHERE id = 1234;
Is there any way to access this functionality via the Django ORM? I.e. incrementally add to an existing SearchVectorField value rather than reconstruct from scratch?
The issue I'm having is the SearchVectorField property returns the tsvector as a string. So when I use the || operator as +, eg:
from django.contrib.postgres.search import SearchVector
instance.my_tsvector_prop += SearchVector(
["new", "fields"],
weight="A",
config='english'
)
I get the error:
TypeError: SearchVector can only be combined with other SearchVector instances, got str.
Because:
type(instance.my_tsvector_prop) == str
A fix to this open Django bug whereby a SearchVectorField property returns a SearchVector instance would probably enable this, if possible. (Although less efficient than combining in the database. In our case the update will run asynchronously so performance is not too important.)
MyModel.objects
.filter(pk=1234)
.update(my_tsvector_prop=
F("my_tsvector_prop") +
SearchVector(
["new_field_name"],
weight="A",
config='english')
)
)
Returns:
FieldError: Cannot resolve expression type, unknown output_field
Another solution would be to run a raw SQL UPDATE, although I'd rather do it through the Django ORM if possible as our tsvector fields often reference values many joins away, so it'd be nice to find a sustainable solution.

MongoDB Python: Unable to remove a field that has a . in it

I'm using pymongo and I'm unable to remove a field that has a "." in the field name. I've tested this same code with other fields that do not have a period and it works without an issue.
db.city.update_many({}, {'$unset': {'AverageDailyPM2.5':1}})
I'm pretty certain I'm on the latest versions of everything including MongoDB 4.4.9.
That is one of the reasons it is not recommended to use "." in a field name.
If you use the pipeline form of update, you can convert the object to an array with $objectToArray so the fields become data, remove the desired field from the array with $filter, convert back to an object with $arrayToObject, and then use $replaceRoot to make that the final document.

Odoo Field Access 3 Dots? Accessing a field of a field? This does not make sense

I am confused by this syntax in the Python wrapper to the Odoo Object-Relational Mapping (ORM). I understand this is one way to access a field: table_name.field_name. However, I am not sure what this means table_name.field_name.field_name. I am not sure I am reading this correctly. This may not mean what I think the code means.
We are using the Odoo 11 API. The documentation for this topic, "field access", can be seen at the following URL: https://www.odoo.com/documentation/11.0/reference/orm.html#field-access
They even give an example but do not give a detailed explanation or a table definition of the relational database table they are referencing via Python.
>>> record.name
Example Name
>>> record.company_id.name
Company Name
>>> record.name = "Bob"
>>> field = "name"
>>> record[field]
Bob
I am confused by the line record.company_id.name. It seems safe to assume that 'record' is the table name. I would guess that 'company_id' is a column name in the 'record' table. But then if that is true, why is 'name' appended to 'company_id' with the dot operator? This is where I am confused.
I understand the code that accesses the "name" field in the "record" table like this: record.name. However, the field access with three dots record.company_id.name does not seem logical. Seems like this is trying to access a field of a field? 🤯
Thanks for reading this. Please let me know how I can improve.

Insert into sqlite3 a null date from pyramid framework using sqlalchemy

python 2.7
pyramid 1.3a4
sqlalchemy 7.3
sqlite3.7.9
from sqlite prompt > I can do:
insert into risk(travel_dt) values ('')
also
insert into risk(travel_dt) values(Null)
Both result in a new row with a null value for risk.travel_dt but when I try those travel_dt values from pyramid, Sqlalchemy gives me an error.
In the first case, I get sqlalchemy.exc.StatementError:
SQLite Date type only accepts python date objects as input
In the second case, I get Null is not defined. When I use "Null", I get the first case error
I apologize for another question on nulls: I have read a lot of material but must have missed something simple. Thanks for any help
Clemens Herschel
While you didn't provide any insight into the table definition you're using or any example code, I am guessing the issue is due to confusing NULL (the database reserved word) and None (the Python reserved word).
The error message is telling you that you need to call your SQLA methods with valid python date objects, rather than strings such as "Null" or ''.
Assuming you have a Table called risk containing a Column called travel_dt, you should be able to create a row in that table with something sort of like:
risk.insert().values(travel_dt=None)
Note that this is just a snippet, you would need to execute such a call within an engine context like that defined in the SA Docs SQL Expression Language Tutorial.

Django saving form strings in database with extra characters (u'string')

I've been having problems in django while trying to save the form.cleaned_data in a postgres database.
user_instance.first_name = form.cleaned_data['first_name']
the data is being saved this way (u'Firstname',) with the 'u' prefix and parenthesis like if I were saving a tuple in the database.
I've used this tons of times with a mysql database and never happened before,
My django version is 1.3.1
UPDATE
i was using commas this way
user_profile.phone_area = phone_area,
user_profile.phone_number = phone_number,
user_profile.email = email,
I edited someone else's source code and forgot to delete the commas, that's why it was generating tuples. Thank you for your help
Aside from validation, form.clean_data() will perform some implicit conversions to Python data types. You can simply perform an explicit conversion by wrapping the returned value with the str() or the unicode() built-in. Afterwards, format the string using strip("(''),").

Categories