How to create initial_data Json fixture for many-to-many relation? - database

I am creating an initializing file for for my django project database. I am doing this using a file called initial_data.json which i have created. For example the following code (when syncdb is run) creates in the model Word a new row where name="apple":
[ { "model": "sites.word", "pk": 1, "fields": { "name": "apple" } } ]
I have managed to this so far for several models, the problem is with models that have a many-to-many field. I've looked around for the correct way to do this and have come up empty.
So, for example, if a Model mood has many Interests how would I write in the Json file that mood-1's interests are interest-1, interest-2 and interest-3.
What is the proper way to write in Json a models many-to-many relation?
EDIT:
#pastylegs solution was correct, I was just having trouble because the numbering of my interests was off in the Json file so it couldn't match them with there moods.

I'm pretty sure the manytomany field of your model can be written like a simple list:
[
{
"model": "sites.word",
"pk": 1,
"fields": {
"name": "apple",
"my_m2m_field_name": [1,2,3],
}
}
]
where 1, 2, 3 are the primary keys for the relations

what I like to do is use the dumpdata command. I fire up a test site, use the admin form or the app itself to add just the data that I want to use in my fixture, then I run
./manage.py dumpdata appname > appname/fixtures/initial_data.json
You can dump all the apps together if you leave out appname but I like to do it separately for each model.
If you're using 1.3 (I'm not yet) then you can use --exclude to not dump some parts. I've aslo seen there is a --indent option to make the output pretty (just found that now while answering your question).
It's one of those things that's easy to miss in the documentation. ;-)

Related

How to handle POST call with JSON having data with "parent-child" relationship using ASP.NET Web API Entity Framework?

I have a JSON which has a format as given below:
{
"header": {
"source_code": "S12345"
"user_id": "987456"
},
"body":{
"source_code": "S12345",
"wrapper_list": [
{
"item_wrapper_code": "WRAP01",
"item_amount": 10,
"item_type_amount": 2,
"creation_date": 20191115,
"worker": "W001",
"workstation_no": "1"
"item_list":[
{
"item_code": "I001"
"item_id": "",
"bar_code": "123987456"
"remark": ""
},
{
"item_code": "I002"
"item_id": "",
"bar_code": "213987456"
"remark": ""
}
]
}
]
}
}
The body of the above JSON object contains data in a parent - child relationship.
Parent: WRAP01
Child1: Item I001
Child2: Item I002
I must be able to parse this JSON object and insert these details as rows into respective tables, i.e., Parent related stuff into Parent table and Children related stuff into Child table.
I request professionals here to kindly guide me.
So, parsing Json to Entity Framwork objects is bad practise.
Firstly, create data transfer objects (DTO) that will be maps to Json.
It is like:
RequestDto, BodyDto, WrapperDto, WrapperItemDto.
For parse it you can use something like newtonsoft, see example: https://www.newtonsoft.com/json/help/html/DeserializeObject.htm
Then you should write EntityFramework objects that will be maps to Database tables.
I hope, you knows Databases theory and already reads some articles about EntityFramework and how it implements different DB relationsips. If no, so please read about it.
You needs One-To-Many relationship. The one-to-many relationship can be configured in the following ways.
By following Conventions
By using Fluent API Configurations
You can use first way. Its more easy. So you need 2 classes: WrapperEntity, WrapperItemEntity.
Then, you should write code that will convert WrapperDto to WrapperEntity and WrapperItemDto to WrapperItemEntity. And then save *Entity objects in database.
It's a best practise for implements such logic. I explain it as top level. For details you can google each question separately.

How to define entities with intent independent in RASA NLU?

I am working in RASA NLU to extract intents and entities in Arabic language, and I have my own entities such as (places, org, and people) and i want to add these entities without any intent.
I just want to add them as an entities and their type.
How can I do this?
Are you using .md or .json as training data file? I cannot think of a solution to pass an entity in markdown format without defining an intent. But in json format you may pass a text without defining an intent by simply not writing a value for the dictionary key "intent". See example below.
The documentation https://rasa.com/docs/nlu/dataformat/ says that intent is an optional field, so it should work.
{
"text": "show me chinese restaurants",
"intent": ,
"entities": [
{
"start": 8,
"end": 15,
"value": "chinese",
"entity": "cuisine"
}
]
}
I would try leaving the value of "intent" completely empty, insert None or Null.

Fetch partial documents from couchdb

I'm using couchdb to store large documents, which is causing some trouble when fetching them to memory. I do realize the database is not meant to be used this way. As a fallback solution, is it possible to fetch partial documents from the database, without creating a view?
In example, if a document has the fields id, content and extra_content, I would like to retrieve only the first two.
Thank you in advance.
If you are using CouchDB 2.x, you can use /db/_find endpoint as a mechanism to retrieve part of the doc.
POST /db/_find
{
"selector": {
"_id": "a-doc-id"
},
"fields": [
"_id",
"content"
]
}
You'll get only the set of fields you have specified in the query
This is not possible prior to CouchDB 2.x. For CouchDB 2.x or greater, see JuanjoRodriguez's answer.
But one possible work around for any version of CouchDB would be to take advantage of file attachments, which by default are excluded from a fetch. If some of your data isn't always needed, and doesn't need to be included in indexes, you could potentially store it as (JSON) attachments, rather than as part of the document directly:
{
"id": "foo",
"content": "stuff",
"extra_content": "other stuff"
}
becomes:
{
"id": "foo",
"content": "stuff",
"_attachments": {
"extra_content": {
"content_type": "application/json",
"data": "ZXh0cmEgc3R1ZmYK"
}
}
}

Append sensor data into document in Couchbase database

We are a group of people writing a bachelor-project about storing sensor data into a noSQL-database, and we have chosen couchbase for this.
We want to store quite a few data in the same document, one document per day, per sensor, and we want to append new sensor data witch comes in every minute.
But unforunatly, we are not able to append new data into existing document without overwriting the existing data.
The structure for the documents is:
DocumentID: Sensor + date, ie: KitchenTemperature20180227
{
"topic": "Kitchen/Temp",
"type": "temperature",
"unit": "DegC"
"20180227130400": [
{
"data": "24"
}
],
..............
"20180227130500": [
{
"data": "25"
}
],
}
We are all new to couchbase and NoSql-databases, but eager to learn and understand how we the best way should implemet this.
We've tried upsert, insert and update commands, but they all overwrite the existing document or won't execute because the document already exists. As you can see, we have some top-level information, like topic, type, unit. The rest should be data coming in every minute and appended to the existing document.
Help on how to proceed would be very appriciated.
Best regards, Kenneth
In this case you can use the subdocument API. This allows you to modify portions of a document based on a "path". This image gives the idea for getting a subdocument.
You can mutate subdocuments as well. Look at the subdocument API documentation for Couchbase. There are also blog posts that go through examples in Java and Go on the Couchbase blog site.

URL with reference to object from HATEOAS REST response in AngularJS

I am using #RepositoryRestResource annotation to expose Spring JPA Data as restful service. It works great. However I am struggling with referencing specific entity within angular app.
As known, Spring Data Rest doesn't serialise #Id of the entity, but HAL response contains links to entities (_links.self, _embedded.projects[]._links.self) like in the following example:
{
"_links": {
"self": {
"href": "http://localhost:8080/api/projects{?page,size,sort}",
"templated": true
}
},
"_embedded": {
"projects": [
{
"name": "Sample Project",
"description": "lorem ipsum",
"_links": {
"self": {
"href": "http://localhost:8080/api/projects/1f888ada-2c90-48bc-abbe-762d27842124"
}
}
},
...
My Angular application requires to put kind of reference to specific project entity in the URL, like http://localhost/angular-app/#/projects/{id}. I don't think using href is good idea. UUID (#Id) seems to be better but is not explicitly listed as a field. This is point I got stuck. After reading tons of articles I came up with 2 ideas, but I don't consider neither of those as a perfect one:
Idea 1:
Enable explicitly serialisation of #Id field and just use it to reference to the object.
Caveat: exposing database specific innards to front-end.
Idea 2:
Keep #Id field internal and create an extra "business identifier" field which can be used to identify specific object.
Caveat: Extra field in table (wasting space).
I would appreciate your comment on this. Maybe I am just unnecessarily too reserved to implement either of presented ideas, maybe there is a better one.
To give you another option, there is a special wrapper for Angular+Spring Data Rest that could probably help you out:
https://github.com/guylabs/angular-spring-data-rest

Resources