In Python, you can assign an arbitrary attribute from outside the defining class :
class Profile(models.Model): user = models.OneToOneField(User) name = models.CharField(max_length=140) p = Profile() p.age = 42
The __dict__ attribute, which supports the dictionary of all attributes, is used as the main mechanism.
We were all told not to expose our internal work to the client code , but attaching new data is not necessarily related to encapsulation, right? Is this idiom common to Python code?
Just what I mean ...
Each Tweet has standard fields, such as id , text , owner .
When you return a list of tweets to a user, you want to display if that tweet is “profitable” by that user.
Obviously, to get is_favorite you need to query the many-to-many relationship for this user .
Would it be ok to preinstall Tweet objects with is_favorite matching the current user ?
Of course, I could set the is_favorite_for(user) method, but I click on the Django template language , which does not allow calling methods with arguments from within the template. In addition, I believe that a template should not call methods at all .
I know this will work fine, but I wonder if something like this in an open source project will force other developers to look at me with contempt.
Sidenote:
I come from C # /. NET, where dynamic types were introduced recently and are not widely adapted, with the exception of some areas of niche (interaction, IoC framework, REST client framework, etc.).