2

I'm new to Python and Django, and I'm wondering how you would query the database without using a model.

I have a table of ignored words, which consists of a primary key and a string of the word. How would I query in my text to "ignore" the word, if it's in the table?

All of the documentation and examples involve models, for say a Person object. But I don't know a model object for an ignored word, so it doesn't make sense.

def determine_important_words(original_words):
    for i, word in enumerate(original_words):
        if word[0].isupper():
            if word not in important_words:
                important_words.append(original_words[i])
    return important_words

The table I have is using MySQL.

Thanks!

1
  • Django is python, so you can use the mysqldb module and run your own queries. Commented Nov 20, 2013 at 13:50

2 Answers 2

4

Your question is a little confusing. You say you have a table with an ID and a string, but you can't make a model for an ignored word "so it doesn't make sense". Why not? What's wrong with:

class IgnoredWord(models.Model):
    word = models.CharField(max_length=200)

Then you can simply query it as normal:

if not IgnoredWord.objects.filter(word=current_word).exists():

If you really can't do that, you can look at the very nice Django documentation for running raw queries, but you shouldn't need to do that.

Sign up to request clarification or add additional context in comments.

1 Comment

It was just that the examples I had found online and in books, referred to models as more of an object (coming from a Java background). It just felt a little bit overkill... but thank you! I will try your solution.
1

Create a simple model for your table:

class IgnoredWord(models.Model):
    word = models.CharField(max_length=200)

Create a list containing all your ignored words:

ignored_words = (obj.word for obj in IgnoredWord.objects.all())

Use it for something:

result = list(set(original_words) - set(ignored_words))

Don't forget to run python manage.py syncdb after adding your new model.

Comments

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.