so I have an Entity with a Datetimetz field, with it default format is Y-m-d H:i:sO right?But, in the database I have the format is Y-m-d H:i:s.uO, I don't actually know why that format, but it isn't my database and I have to work with it.
So how I can change the format of the field?
Thanks!
You can define your own types.
Symfony: https://symfony.com/doc/current/doctrine/dbal.html#registering-custom-mapping-types
Doctrine: http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/types.html#custom-mapping-types
The transformation is then performed in the methods called convertToPHPValue and convertToDatabaseValue.
Related
Does loopback 3 support filters on JSON field of a table ? I am using MySQL database.
For example, we create table 'events' -
CREATE TABLE events(
id int auto_increment primary key,
event_name varchar(255),
visitor varchar(255),
properties json,
browser json
);
Is there any way to apply filter on json fields 'properties' and 'browser' ?
First of all, MySQL doesn't index JSON column (there is workaround but not supported natively), if you go with MySQL, it can be problem in the near feature.
It think it is not possible because doc says that:
Data source connectors for relational databases don’t support filtering nested properties.
However you can implement your own logic for build-in methods via using operation hooks.
For example
Model - the constructor of the model that will be queried
query - the query containing fields where, include, order, etc.
MyModel.observe('access', function **queryToJsonField**(ctx, next) {
// operation goes here
next();
});
More detailed explanation can be found at doc
Is there a way to query an object, 'extract' a nested piece of data from a JSONField field and then make it available as a custom, temporary field on each instance of the Queryset?
In my use case, I'm storing overflow metadata from Twitter's API in a data field for later use. I'd like to be able to access the nested field followers_count within TwitterPost.data.
I've read the docs about how to filter based on nested values but not how to extract it as a temporary field when generating a queryset.
Similarly, I've read the annotate docs for ways to create a custom temporary field but the examples all use aggregation functions on simple fields, so not JSONFields.
Thanks in advance for any suggestions.
Example model:
from django.contrib.postgres.fields import JSONField
class TwitterPost(models.Model):
id = models.IntegerField()
data = JSONField()
Example JSON value for the data field:
{
'followers_count': 7172,
"default_profile_image": false,
"profile_text_color": "000000"
}
Pseudocode for what I'd like to be able to do:
TwitterPost.objects.annotate(followers_count=instance.data.followers_count)
This is probably a late answer, but there is a way to do it
from django.contrib.postgres.fields.jsonb import KeyTransform
TwitterPost.objects.annotate(followers_count=KeyTransform('followers_count', 'data'))
OR KeyTextTransform could be used instead of KeyTransform (for converting to string)
If you want to access the data inside a JSONField, you've to use __. In your example it will be something like this
TwitterPost.objects.annotate(followers_count=instance.data__followers_count)
Take a look to the documentation here
I have a proxy model which has to have some calculated (with query expressions) read-only fields. Annotating won't do, because I will later need to have field metadata to do filtering in my views.
So, is there a way to call a SQL function to get the value?
I am looking for something like a QuerySet.annotate for fields, a Transform or a custom field that would do this.
I found a way to do this by using the Col Expression and a custom field.
Why doesn't Django in SQL table django_admin_log use TIMESTAMP instead of DATETIME for attribute action_time? I mean according to this answer it should be using TIMESTAMP.
LogEntry's action_time field is a models.DateTimeField, which (perhaps unsurprisingly) is stored as a DATETIME field.
I think you've misunderstood the answer you linked - it recommends using DATETIME, not TIMESTAMP:
If you meant that you want to decide between using a UNIX timestamp or a native MySQL datetime field, go with the native format. You can do calculations within MySQL that way ("SELECT DATE_ADD(my_datetime, INTERVAL 1 DAY)") and it is simple to change the format of the value to a UNIX timestamp ("SELECT UNIX_TIMESTAMP(my_datetime)") when you query the record if you want to operate on it with PHP.
I have a model that contains a FileField. I want to search for a specific filename. How do I do it? I was trying:
MyModel.objects.get(document__name=FOO)
I got a Join on field 'document' is not permitted.
Thanks!
The attributes of a FileField are not stored in the database, and cannot be used in a query. For example, the name is simply the upload_to string plus the filename. If you want to store extra data about the file you have to put that data into other fields on the database, as the example documentation shows with a Car having a name of "57 Chevy".
Also, typically the double underscore in Django's ORM denotes following a database relationship, either a ForeignKey or a ManyToMany. So in the example ORM call you provided, I would assume that MyModel had a field document that was either a ForeignKey or ManyToMany to another model, and that other model has a field called name. Which doesn't sound like is the case.
Hope that helps some.
Do this instead:
MyModel.objects.get(document__icontains='FOO')
You can filter on document, and it'll filter by the string that is the path on disk to the file.