0

I have a many-to-many relationship between a link and a term in Django, but when trying to check if the relationship already exists between the link and term I'm getting an undefined variable on the method.

def create_models(my_term, link):
    obj, created = WikifyProjectTerms.objects.get_or_create(term = my_term)

With my models as:

class WikifyProjectTerms(models.Model):
    id = models.IntegerField(primary_key=True) # AutoField?
    term = models.CharField(max_length=100)

class WikifyProjectLinks(models.Model):
    id = models.IntegerField(primary_key=True) # AutoField?
    links = models.CharField(max_length=100)

class WikifyProjectLinksTerms(models.Model):
    id = models.IntegerField(primary_key=True) # AutoField?
    links = models.ForeignKey(WikifyProjectLinks)
    terms = models.ForeignKey('WikifyProjectTerms')

I have pasted the line from the django documentation https://docs.djangoproject.com/en/1.4/ref/models/querysets/#get-or-create (and tailored it) but Eclipse is insisting it does not exist, for any of my models. The update_or_create() method doesn't exist either.

Any ideas will be appreciated!

This isn't a runtime error, this is Eclipse complaining: Screenshot

4
  • But WikifyProjectLinksTerms indeed doesn't have term or link attributes... Commented Dec 18, 2013 at 11:10
  • I forgot to change that from trying with another model. It's not the attributes that don't exist, it's the method from Model.objects Commented Dec 18, 2013 at 11:11
  • It's impossible to tell what is causing the error here. That's why you should post the actual error message and the traceback. Commented Dec 18, 2013 at 11:18
  • There is no actual message, like a stacktrace. I've included a screenshot of what Eclipse is complaining about. As far as it goes the code won't run. Commented Dec 18, 2013 at 11:22

1 Answer 1

2

Python, as I've had occasion to say once or twice before, is not Java.

One of things that makes it not-Java is that class attributes are defined dynamically. It is not always possible to determine what attributes an object has except at run-time.

Eclipse, or any other IDE that attempts to do static analysis of your code, is not reliable as a guide to what attributes do or do not exist on a class.

That's why many Python programmers prefer a simpler text editor - from vim to SublimeText - rather than an IDE. Don't forget, you have the Python shell which you can use to explore objects, using for example the dir() command, which will show you what an object actually has rather than what an IDE thinks it has.

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

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.