Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7748451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:47:19+00:00 2026-06-01T10:47:19+00:00

I’m currently using django with tastypie. I need to implement an api for Questions

  • 0

I’m currently using django with tastypie. I need to implement an api for Questions and answers. I’ve created the following resources tied to the Question and Answer model respectively:

class QuestionResource(ModelResource):
    answers  = fields.ToManyField("material.resources.AnswerResource",
                                "answer_set",blank=True,full=True)

class AnswerResource(ModelResource):
    question = fields.ToOneField(QuestionResource,'question')

In my mind answers belong to questions, and a user shouldn’t be able to create a question without the corresponding answers. In my previous iteration I used a django formset to contain the information both for the question and the related answers. With tastypie, there’s not a neat out-the-box solution (as far as I know) for this scenarion.

Should I only allow requests to the question resource which will then update the corresponding answers, or should I couple the 2 resource losely, meaning that the user then has to make sure to submit the associated answers after the question is created. The Former seems to me to be better in principle since the latter can produce question without answers which in principle should never be the case.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T10:47:20+00:00Added an answer on June 1, 2026 at 10:47 am

    Well, I think it’s not possible to create two different resources at once in a RESTful API (because you can’t POST to two different URLs at once). So to keep the API clean and RESTful I’d rather suggest you follow the latter approach and allow for creating them separately (unless you combine them into one resource). This might not be as bad as it seems.

    I understand this may yield questions without answers (I assume the question is the first entity which should appear and this question should then be answered). However in your application you could overcome this by not operating on questions unless answered (e.g. don’t display them anywhere).

    You could even go one step further and define a custom manager class which filters those unanswered question out by default, like so:

    # assumed that `answers` is related_name in ForeignKey from Answer to Question
    class Question( models.Model )
        objects = AnsweredQuestionsManager()
        all_questions = models.Manager()
    
        @property
        def is_answered( self ):
            return self.answers.count() > 0
    
    class AnsweredQuestionsManager( models.Manager ):
        def get_query_set( self ):            
            return super( AnsweredQuestionManager, self )\
                .get_query_set().annotate(
                    num_answers = Count( 'answers' ) ).filter( num_answers__gt = 0 )
    

    then Question.objects.all() would only return questions with answers and Question.all_questions.all() would include unanswered question as well.

    In order to keep your database relatively clean you could get rid of old, unanswered questions periodically (e.g. using Cron).

    I hope this makes you lean towards separation a bit more 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have thousands of HTML files to process using Groovy/Java and I need to
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.