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.
Related
I've got 2 questions, but they are related to the same topic.
I know how to retrieve data from a for loop using template tags
{% for status in status %}
<tr>
<td>{{ status.status}}</td>
</tr>
{% endfor %}
However when I want to retrieve a single object i get an error even when i use:
po = Status.objects.latest('id')
and remove the for loop.
I get:
'Status' object is not iterable
My questions are:
How can I get the latest entry from the database for a given model?
How can I setup my templates tags to allow for just a single record?
You have two different questions here:
How do I retrieve the latest object from the database.
You can do this using the latest() queryset operator. By reading the docs you will note that this operator works on date fields, not integers.
Status.objects.latest('date_added') # or date_updated
If you want to do this off the ID you will need to order by ID and select the first result. (this will only work if you are using incrementing primary keys, it will not work with UUID's or randomly generated hashes).
Status.objects.order_by('id')[0]
Side note: I would personally use the date_added / date_updated way of doing this.
Iterating over a single object
A single object cannot be iterated over. For this you will need to use a different template. Or, you will need to add the single object into a list.
# note the [] around the query
result = [Status.object.latest('date_added')]
Personally I have a different views for listing single / multiple result. I have a ListView for many result objects and a DetailView for single objects.
TableName.objects.filter(key=value).order_by('-date_filed').first()
The "-date_filed'" field will reverse the order and the first will give you the latest element.
Let us assume I have a Model named "OneOfTheModelsUsed"
and there is a field called "car_name" and "date" within this model.
The following code worked for me while I was using Django FormWizard. After going through all the steps in the form and it gets saved. I used
last_entry = OneOfTheModelsUsed.objects.latest("date")
This gives all the entries in that model
last_car_name = last_entry.car_name
This gives the specific field entry you want in the given form.
return render(request, 'reference.html', {'last_car_name':last_car_name,}
passed the data to a template.
for the display in the template I used
{{last_car_model}}
and if you need the id for that entry.. use this {{last_car_model.id}} in the template.
PS:I'm fairly new to Django and Web development as a whole so I don't know much of the technical terms for all this
This is because latest returns a single instance rather than a queryset (which is iterable). So:
1) Latest is not working because it works with Date Fields. Read more at: https://docs.djangoproject.com/en/1.8/ref/models/querysets/#latest. 'id' is not a valid field to use with the latest filter.
2) You can't use for template tag with a single instance because it is not iterable.
To solve your situation, I would specify the ordering = ('id',) field in the Meta class of the model and then do a po = Status.objects.all()[:1] so you will obtain a queryset (which is iterable) with a single object in it. Then you will be able to use the for template tag with your po variable.
Hope it helps.
comments = usercomment.objects.order_by('-id')[:100]
I Did By This Method
First "comment" Is The Variable Name, "usercomment" Is Model Name, "id" is generated by django when make a model, [:100] is from 0 to 100 you can increase the number to x
I have created a module which modifies other one (named base). In the module base there is the res.partner model, and in this model there is the field birthdate:
_columns = {
...
'birthdate': fields.char('Birthdate'),
...
}
What I do in my module is to overwrite this field to make it of type Date:
birthdate = fields.Date('Birthdate')
Everything seems OK, but, after updating the Odoo server, the data introduced in that column dissapears from the view, and when I check the database, I find that the column birthdate is being duplicated with other names like birthdate_moved0, birthdate_moved1, birthdate_moved2, etc... (and half of them are of type char and the other half of type date). The values stored in birthdate are being moved to these other columns (that's the reason bacause I can't see the data in the view, since in the form only birthdate is being shown).
However, I was able to overwrite several fields through Python. But this duplication problem happened me with the field birthdate and the field function of the model res.partner.
I can't come to a conclussion. Can anyone help me here, please? Thank you in advance!
You should name your "new" field 'birth_date' or 'dob' or anything other than 'birthday' just to avoid changing existing field data type. In next step you can copy values from current 'birthday' field to new one (through postgresql).
Finally, a co-worker shown me the solution:
It's not about Odoo, it's due to PostgreSQL. Generally, in PostgreSQL, is not possible to alter the data type of a column (even when this is empty), except for some cases, like for example:
From integer to char: because the casting to char is possible. Therefore, in Odoo, when you change the data type of a field.Date, a field.Integer, a field.Many2one, etc... to a fields.Char, there is no problem. However, if you try to change a fields.Char to fields.Date or fields.Many2one, or whatever, PostgreSQL is going to duplicate the column because is not ready for that type of casting.
That's the reason because I wasn't able to change a field of type Char and transform it in a field of kind Date (my attempt with birthdate) or Many2one (my attempt with function). And on the other hand, I was able to overwrite a Selection field (actually, in PostgreSQL is convert a Char into another Char).
So in conclusion:
If you are going to change the type of data of a field, check if the final kind of data is char (fields.Char, fields.Selection, etc...) or other possible casting. Then you can name the new field with the same name as before. If not, you must name the new field with other name, otherwise PostgreSQL will duplicate the column with name_moved0, name_moved1, etc...
I hope this helps to anyone!!
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)
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("(''),").
Schema(title=TEXT(analyzer=stem_ana, stored=True),
content=TEXT(analyzer=stem_ana, vector=True),
link=ID(stored=True),
meta=TEXT(analyzer=stem_ana),
path=ID(stored=True),
scores=ID(stored=True),
clicks=NUMERIC(stored=True),
file_name=ID(unique=True)
)
I am unable to retrieve the field content=TEXT(analyzer=stem_ana, vector=True)
I wan't to update the content of clicks without altering the other fields, how should I do it?
I have no clue how to retrieve the contents of the fields that are not stored.
whoosh can't update indexed documents "in place" (just imagine one field got longer...).
So, you'ld retrieve the document from whoosh index (it will include all stored fields).
If you have fields that are not stored into whoosh but kept elsewhere, you'ld need to retrieve them from these other places and add them to the document again.
Then call update_document(**fields) - whoosh will use the unique fields to remove the old indexed documents that have the same values in these fields and then index the new document.