Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango,mongodb,Tastypie-nonrel : List of ForeignKey
    primarykey
    data
    text
    <p>On my Django-mongodb model, I would like to have an object with a listField containing reference to other objects. Here is an example of what I want to achieve :</p> <p>models.py</p> <pre><code>class Comment(models.Model): title = models.CharField(max_length=50) body = models.CharField(max_length=50) class Post(models.Model): name = models.CharField(max_length=50) commentList = ListField(models.ForeignKey(Comment)) </code></pre> <p>api.py (Tastypie Resources)</p> <pre><code>class CommentResource(MongoResource): class Meta: object_class = Comment queryset = Comment.objects.all() resource_name = 'comment' authentication = Authentication() authorization = Authorization() class PostResource(MongoResource): commentList = ListField(models.ForeignKey('CommentResource', 'commentList') #Wrong but just the expression of my incomprehension. class Meta: object_class = Post queryset = Post.objects.all() resource_name = 'post' authentication = Authentication() authorization = Authorization() </code></pre> <p>In this example, the field "commentList" contains a list of "Object ID" referencing "Comment" objects. If nothing is done, an HTTP GET on my "Post" resource will give me:</p> <pre><code>[...], objects: [ { id: "4f47b159c789550388000000", name: "Hello World", commentList: "[u'4f47b14ec789550387000000']", resource_uri: "/api/v1/post/4f47b159c789550388000000/" } ] </code></pre> <p>What I would love to get is this :</p> <pre><code>[...], objects: [ { id: "4f47b159c789550388000000", name: "Hello World", commentList: [ comment:{ title : "My comment title", body : "It would be great if tastypie-nonrel could do this!", resource_uri: "/api/v1/comment/454f4v59c789550388051486/" } ], resource_uri: "/api/v1/post/4f47b159c789550388000000/" } ] </code></pre> <p>My question is : How can I resolve the reference to objects <strong>Comment</strong> and make it available with a API call to the ressource <strong>Post</strong>?</p> <p>If this is not possible, what would be the best way to design my non-relational data model so that a <strong>Post</strong> can contain multiple <strong>Comment</strong>, but that <strong>Comment</strong> can alo be accessed on its own and updated independently?</p> <p>Thanks a lot for your help!!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload