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 6560457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:27:45+00:00 2026-05-25T13:27:45+00:00

I have a model ‘Objective’, where the usual way to refer to objects is

  • 0

I have a model ‘Objective’, where the usual way to refer to objects is by the unicode method.

models.py:

class Objective(models.Model):
    level = models.IntegerField()
    strand = models.ForeignKey(Strand)
    order = models.IntegerField()
    description = models.TextField()
    def __unicode__(self):
        return u'%s%s%s' % (self.level, self.strand.code, self.order)

    class Meta:
        unique_together = ("strand", "level", "order")
        ordering = ['level', 'strand', 'order']

An example object in this model would be called e.g. 6ssm4, for the fourth entry in the ssm strand at level 6. I want to do my lookups (say from url parsing) by referring to this unicode string.

urls.py:

(r'^(?P<objective>[^/]+)/$', 'display_objective'),

I have tried all of the following variations as lines in views.py (not all at once!):

def display_objective(request, objective):
    theobjective = Objective.objects.get(unicode() = objective)
    theobjective = Objective.objects.get(self.unicode = objective)
    theobjective = Objective.objects.get(__unicode__ = objective)
    theobjective = Objective.objects.get(objective__iexact = objective)
    theobjective = Objective.objects.get(objective)
    theobjective = Objective.objects.get(unicode() = objective)

But if I go to http://localhost:8000/6ssm4/ I get the error page with errors like “Keyword can’t be an expression” or “Cannot resolve keyword ‘self’ into field. Choices are: assessment, description, id, level, order, strand”.

Is this a legitimate way to lookup objects, or should I be dissecting the keyword? If it is legit, what is the correct syntax?

  • 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-05-25T13:27:46+00:00Added an answer on May 25, 2026 at 1:27 pm

    The ORM converts filter lvalues (the things on the left side of the equals sign) into SQL lookup terms, which is why it has a limited syntax. You can’t make a Python function a SQL lookup term; the database knows nothing about Python.

    One correct way to do this would be to create a new field in your table, call it index or lookup, and then do this:

    def save(self, *args, **kwargs):
        self.lookup = self.__unicode__()
        super(Objective, self).save(*args, **kwargs)
    

    The lookup field becomes a pre-processed member of your table; every time you save an Objective, a lookup-ready version of it gets stored in the table. You can then:

    theobjective = Objective.objects.get(lookup = objective)
    

    Your alternative is to break the objective into its component parts and filter on level, strand, and order, but that only works if they’re sufficiently regular for a regular expression.

    This is a classic tradeoff of programmer time vs. responsiveness vs. storage (pick two). In this case, I’ve made the call that programmer time and responsiveness are more important that storage considerations.

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

Sidebar

Related Questions

Lets say I have model inheritance set up in the way defined below. class
I have model with a Foreign Key to itself like this: class Concept(models.Model): name
I have model like this: class Kaart(models.Model): name = models.CharField(max_length=200, verbose_name=Kaardi peakiri, help_text=Sisesta kaardi
I'm newcomer in django, and here is question: I have model class: def Client(models.User)
If I have Model.objects.all() I want to get only one object for any content_object=foo,
I am newbie at Django. I have model with a custom method. In view
I have some model objects I'm using in my Java client application. Later these
I have model with ImageFile field: def upload_course_cover(object, filename): return '/media/courses/%s_%s' % (Course.objects.aggregate(Max('id'))['id__max'] +
I have model public class MyModel { public string Name { get; set;} }
I have: 1) public class Model { public String Name { get; set; }

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.