Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy can't I use __getattr__ with Django models?
    primarykey
    data
    text
    <p>I've seen examples online of people using <code>__getattr__</code> with Django models, but whenever I try I get errors. (Django 1.2.3)</p> <p>I don't have any problems when I am using <code>__getattr__</code> on normal objects. For example:</p> <pre><code>class Post(object): def __getattr__(self, name): return 42 </code></pre> <p>Works just fine...</p> <blockquote> <pre><code> &gt;&gt;&gt; from blog.models import Post &gt;&gt;&gt; p = Post() &gt;&gt;&gt; p.random 42 </code></pre> </blockquote> <p>Now when I try it with a Django model:</p> <pre><code>from django.db import models class Post(models.Model): def __getattr__(self, name): return 42 </code></pre> <p>And test it on on the interpreter:</p> <blockquote> <pre><code> &gt;&gt;&gt; from blog.models import Post &gt;&gt;&gt; p = Post() ERROR: An unexpected error occurred while tokenizing input The </code></pre> <p>following traceback may be corrupted or invalid The error message is: ('EOF in multi-line statement', (6, 0))</p> <p>--------------------------------------------------------------------------- TypeError<br> Traceback (most recent call last)</p> <p>/Users/josh/project/ in ()</p> <p>/Users/josh/project/lib/python2.6/site-packages/django/db/models/base.pyc in <strong>init</strong>(self, *args, **kwargs) 338 if kwargs: 339 raise TypeError("'%s' is an invalid keyword argument for this function" % kwargs.keys()[0]) --> 340 signals.post_init.send(sender=self.<strong>class</strong>, instance=self) 341 342 def <strong>repr</strong>(self):</p> <p>/Users/josh/project/lib/python2.6/site-packages/django/dispatch/dispatcher.pyc in send(self, sender, **named) 160 161 for receiver in self._live_receivers(_make_id(sender)): --> 162 response = receiver(signal=self, sender=sender, **named) 163 responses.append((receiver, response)) 164 return responses</p> <p>/Users/josh/project/python2.6/site-packages/photologue/models.pyc in add_methods(sender, instance, signal, *args, **kwargs) 728 """ 729 if hasattr(instance, 'add_accessor_methods'): --> 730 instance.add_accessor_methods() 731 732 # connect the add_accessor_methods function to the post_init signal</p> <p>TypeError: 'int' object is not callable</p> </blockquote> <p>Can someone explain what is going on?</p> <hr> <p>EDIT: I may have been too abstract in the examples, here is some code that is closer to what I actually would use on the website:</p> <pre><code>class Post(models.Model): title = models.CharField(max_length=255) slug = models.SlugField() date_published = models.DateTimeField() content = RichTextField('Content', blank=True, null=True) # Etc... Class CuratedPost(models.Model): post = models.ForeignKey('Post') position = models.PositiveSmallIntegerField() def __getattr__(self, name): ''' If the user tries to access a property of the CuratedPost, return the property of the Post instead... ''' return self.post.name # Etc... </code></pre> <p>While I <em>could</em> create a property for each attribute of the Post class, that would lead to a lot of code duplication. Further more, that would mean anytime I add or edit a attribute of the Post class I would have to remember to make the same change to the CuratedPost class, which seems like a recipe for code rot.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload