user764357
user764357

Reputation:

How do you have an attribute called "property" and use the @property decorator?

I have a bit of a problem, and I've tried googling, but it doesn't turn up anything helpful.

I'm designing a django application and I want/need to have a field called "property". The reason for this is that is the technical title of the thing I'm trying to manage, and where possible I'd like to keep the business terminology.

Now this hasn't been a problem... up until now.

I now need to have a method, that I'd like to be able to use as a property, but there is a bit of a clash around the use of the token property.

class DataElementConcept(trebleObject):
    template = "polls/dataElementConcept.html"
    objectClass = models.ForeignKey(ObjectClass,blank=True,null=True)
    property = models.ForeignKey(Property,blank=True,null=True)

    @property
    def registryCascadeItems(self):
        return [self.objectClass,self.property]

I understandably get this error.

  File "/home/theodore/Github/possum-mdr/polls/models.py", line 381, in DataElementConcept
    @property
TypeError: 'ForeignKey' object is not callable

Is there a way around this that lets me treat this method as a property, and keep the property attribute?

Upvotes: 6

Views: 465

Answers (2)

shx2
shx2

Reputation: 64318

Use the "fully-qualified" name of the builtin property:

import __builtin__

@__builtin__.property
def registryCascadeItems(self): ...

Of course, I used quotations around the term "fully-qualified" because this is not a pythonic term. There are no name-qualifications in python. What this really is, is just another place where the original property is defined and can be accessed.

IMHO, this also makes your code more readable. There is no doubt what kind of property is involved -- you explicitly refer to the builtin one.

Upvotes: 6

Mikko Ohtamaa
Mikko Ohtamaa

Reputation: 83438

Maybe this helps:

# On the module-level, do a workaround reference
# as property() is just a normal built-in function
_property = property 

class Foobar:
   property = ....


   @_property
   def registryCascadeItems(self):
       ....

Upvotes: 9

Related Questions