0

I would like to know if there is a way to return a substring in django after retrieving a chunk of data from a column in the database. In this case, I'm trying to retrieve data from the 'contents' column to manipulate the text available and return it.

Retrieval of question list method

def prepare_questions(subject_id, scope, sort, topic, subtopic):
   question_list = Question.objects.filter(subject=subject_id)
return question_list

Model that contains contents

class Question(models.Model):
   subject = models.ForeignKey(Subject)
   content = models.TextField(verbose_name=_('Content'))

What I hope to do is to take question_list's data from 'contents', pass it through a substring method in python. I'm unsure of the steps I should take from here. In addition I would like the method result to be returned as part of the question_list with a new key tagged to the method return value as I do not want to override the original 'contents' value retrieved.

Any pointers or references greatly appreciated! Thanks!

2
  • your description of what you hope to do is quite unclear. can you give an example of some input and output data? Commented Jul 31, 2012 at 16:59
  • Can you give more detail about what is stored in the Content column? What are you parsing in each entry of Content list? Sample data would be a great way to explain what you are trying to achieve. Commented Jul 31, 2012 at 17:05

2 Answers 2

2

Define a method on your model:

class MyModel(models.Model):
    ...
    def contents_substring(self):
        # do stuff
        return the_substring

Then any time you access an instance, whether on it's own or in a loop, you can just do:

instance.contents_substring()

Or in a template:

{{ instance.contents_substring }}
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, that's a better, simpler way to do it. :-)
1

Is this what you are looking for:

return [{'substring': substring_method(q.content), 'question': q} for q in question_list]

Naturally you'll have to define or replace substring_method with whatever Python substring method you want.

What this line does is uses Python's "list comprehension" syntax to map each Question object in question_list into a dictionary with two items, i.e. substring (the substring) and question (the original Question object).

Here's how you might use the new prepare_questions:

questions = prepare_questions( ... )  # whatever arguments
first_question = questions[0]  # let's assume there's at least 1
print first_question['substring']  # prints the substring of the first question's content
print first_question['question'].content  # prints the first question's full content

For completeness, here's a simple example 'substring_method':

def substring_method(s):
    return s[1:4]  # use Python slice syntax to return 2nd through 4th chars of string

1 Comment

Hi, thanks for your input, it looks feasible but I used Chris Patt's solution :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.