Backbone-relational with Django - django

I'm building a Backbonejs app that is running Django in the backend. In my Django, i have models like Author, books, shelf and user and they are related to one another. In Backbone, I have a model Author and when I do a fetch() I get its related models in an array. Should I proceed like this or it's better to create the same models in Backbone and do the same relations between them? (with backbone-relational)
Also, let's say I go with the second option, when i do fetch() and get related models, shall backbone-relational recognize it directly?
Thanks

I would recommend leveraging Backbone relational--especially if you expect the app to get somewhat complex later.
Probably you won't have to change your server side code, you can get Backbone relational to instantiate any related models that are included in the JSON of the model you have fetched. So if for an author query your backend returns:
[{
name: "Hemingway, Ernest",
books: [
{name: "For whom the bell tolls", ISBN: 1234},
{name: "The sun also rises", ISBN: 2345}
]
}]
... and you have defined a has-many relationship between Authors and books Backbone relational will instantiate the books as well.

Related

How to set conditions on "through" model defined by "hasManyThrough" relation in Loopback

I defined 3 models in Loopback: job, contact and job_contact(through model) and using hasManyThrough relations defined below relation:
job has many contact through job_contact.
and I used the code below to find contacts by job through job_contact
job.findById(id, {
include: {
relation:'contact',
where :{deleted: false}, // no working here
scope:{
where:{deleted: false} // here will add condition on contact table
}
}
})
Someone got any ideas? How can I put conditions on "through" model, job_contact model, in this case?
I have found it is not possible to interact with the through model with filters on queries the two other models. If you want to access the through model you need to query it independently.
See part More Info at the bottom of the answer here and the linked github discussion.

Error database query using ManyToManyField on model

I'm fairly new to django web development. And I got an error whereby I try to change a 'post' under admin url - so localhost:8080/admin. I'm able to create it successfully but when I try to click the post that I had just added. I'm getting this error:
Exception Type: DatabaseError Exception Value: This query is not
supported by the database.
And this is the code that I know is 'messing' with this query:
#Post is an abstract class
class BlogPost(Post):
...
translators = models.ManyToManyField(Staff, related_name='translators')
photographers = models.ManyToManyField(Staff, related_name='photographers')
authors = models.ManyToManyField(Staff, related_name='authors')
...
To explain what is going on with this blog post - it can have multiple 'owners'/people that contributed to this post and thus the decision using ManyToManyField. And vice-versa with the 'Staff' member - the type of 'member' can have multiple ownership on multiple posts (Let me know if this logic doesn't make any sense because it does to me).
I'm using mongodb for the database, django 1.5.11 and I have installed djangotoolbox. I've tried the following solutions with adding a relationship to BlogPost as shown below:
Class Staff(Member):
...
staff_posts = models.ManyToManyField(BlogPost, related_name="staff_posts")
...
But I'm getting an error on 'cannot import BlogPost'. I tried figuring out the reason of this error and I don't think that I have a circular dependance - after checking all of the files, there's no circular dependance.
MongoDB (or mongoengine, which I'm guessing you're using) doesn't support joins, so the typical way to model many-to-many relations in a relational database has to be implemented some other way.
One way is to use a ReferenceField inside a ListField. It might look like this (not tested):
class BlogPost(Post):
authors = models.ListField(models.ReferenceField(Staff))
...
Also see these answers:
https://stackoverflow.com/a/18747306/98057
https://stackoverflow.com/a/25568877/98057
Just to put it out there, I'm not real familiar with MongoDB.
However, I don't believe you need to define a ManyToManyField on your Staff class. You already have a ManyToMany defined in your BlogPost, having it defined in one class file is all that is required. (At least for MySQL).

Ember Data: Trying to make a Child Record persist in Rails 4 backend

I've searched a lot for an answer to this, and I feel like I'm close...
I have Quiz, Question, and Answer models.
Rails--
Quiz has_many Questions
Question has_many Answers
Ember--
Quizzmob.Quiz = DS.Model.extend
title: DS.attr('string')
questions: DS.hasMany('question')
Quizzmob.Question = DS.Model.extend
prompt: DS.attr('string')
quiz: DS.belongsTo('quiz')
I'm able to persist a new quiz to my Rails backend just fine with a simple save().
I've also sideloaded the JSON so it looks like this:
{"questions":[
{"id":214,"prompt":"Is this a question?"} //--added manually--//
],
"quizzes":[{"id":185,"title":"First Quiz","questions":[214]},
{"id":186,"title":"Second Quiz","questions":[]},
{"id":187,"title":"Third Quiz","questions":[]},
...
...
]}
Here is a pattern I've seen a couple times in my search for an answer:
Ember QuestionsNewController:
Quizzmob.QuestionsNewController = Ember.ArrayController.extend(
needs: 'quiz'
quiz: Ember.computed.alias('controllers.quiz.model')
prompt: ''
actions:
save: ->
quiz = #get('controllers.quiz.content')
quiz.save()
prompt = #get('prompt')
question = #store.createRecord "question",
prompt: prompt
#set('prompt', '')
questions = quiz.get('questions')
questions.addObject(question)
question.save().then =>
quiz.save()
)
My console shows all successful calls, my server shows successes too:
Processing by Api::V1::QuestionsController#create as JSON
Parameters: {"question"=>{"prompt"=>"new", "quiz_id"=>"185"}}
Completed 200 OK
Processing by Api::V1::QuizzesController#update as JSON
Parameters: {"quiz"=>{"title"=>"First Quiz"}, "id"=>"185"}
Completed 200 OK
A new Model for Question gets created and I see it in my Ember Debugger, but when I refresh the page, the model disappears. I read somewhere that it may have something to do with my serializer, and that I might have to add a 'serializeHasMany' method to customize it...but that seems like a lot of work for a seemingly simple and common task. Not sure if it's the 'ember way'.
Thanks so much for any help. I'll update the question with any additional info that's needed.
I have a similar type of relationship in my database, and I think I see what your problem is. With ember, the relationship is not going to be saved on the hasMany side. So the relationship will be completely defined in your database in the questions table. To find all questions a quiz has, you would have to filter the database based on the paramter that the
{quiz_id: SOME_ID}
Now a question to ask yourself is can the questions on be on one quiz? If they can be on many quizzes, then you have a manyToMany relationship, which would probably require a change to the serializer, as I had to make myself.
In conclusion, the reason that your list of questions isn't being updated or sent to the server, is because you can only add the relationship to the Question itself, so question.set('quiz', some_quiz). You cannot get all questions from the quiz.
I hope this helps, sorry for all the wording.

Ember-Data: Ember does not make the connection between two related models - DS.ActiveModelAdapter

I am trying to retrieve a project which has a hasMany relationship to projectTeams which has a belongsTo relationship to Teams.
I am using ActiveModelAdapter and have followed the conventions indicated in the doc on emberjs.com. After the files have been retrieved, I checked the JSON output and it is valid according to what ember expects (foreign keys contain the _id suffix). However, the relationship between the models is not made.
Interesting fact though is that I can get a reference from ProjectTeam to both Project and Team...
For your convenience I created the following fiddle:
http://jsfiddle.net/alexspeller/SwWkB/1/
You need to define the project team ids on the json for projects
id: 1
name: "asd"
top_down_date: "2014-01-08"
cape_town_date: "2014-01-23"
created_at: "2014-01-04T16:17:59.441Z"
updated_at: "2014-01-04T16:17:59.441Z"
project_team_ids: [1]
http://jsfiddle.net/VHfKv/

Group models in django admin

Is there any way to group the models in django admin interface?
I currently have an app called requests with the following models showing in the admin site:
**Requests**
Divisions
Hardware Requests
Hardware Types
Requests
Software Requests
Software Types
I would like the divisions, Software Requests and Hardware Requests to be grouped separately in a "Types" group.
I know I could override and hard code the admin/index.html and base_site.html but this seems rather convoluted just to specify grouping.
Is there anything i can add to the Meta class to specify a group name?
The only way I have found so far to achieve what i want is to move the models to a new app within requests ("requests.Types") but again, doesn't feel like its the 'correct way'.
You can do it using django-modeladmin-reorder.
You'd configure it somehow along these lines then (settings.py):
ADMIN_REORDER = (
{'app': 'requests', 'label': 'Types',
'models': (
'requests.Divisions',
'requests.HardwareRequests',
'requests.HardwareTypes'
)
},
{'app': 'requests', 'label': 'Other models',
'models': (
'requests.Requests',
'requests.SoftwareRequests',
'requests.SoftwareTypes'
)
},
)
There isn't anything you can put in the model definition to do this, because models -- by design -- don't know about the existence of the admin, or how they'll be presented in the admin.
This really is a case where you just write a template that does what you want, and use it; all you're asking for is a presentational change, and templates are where you do presentation in Django.