开发者

Python property() returning unlisted field?

The code I'm looking at is from django.contrib.auth.models.User

def _get_message_set(self):
    import warnings
    warnings.warn('The user messaging API is deprecated. Please update'
                  ' your code to use the new messages framework.',
                  category=DeprecationWarning)
    return self.**_message_se开发者_开发问答t**
message_set = property(_get_message_set)

--where the devil is this _message_set field?

I suspect something of the automatic sort is happening here, but I'm not sure.


In Python, an attribute of an object does not need to be declared. In your case, it's set up in the constructor of the superclass models.Model.

Note that the internal attribute is prefixed with an underscore, whereas the external one (which happens to be a property) lacks one.


from django.contrib.auth.models:

class Message(models.Model):
    """
    The message system is a lightweight way to queue messages for given
    users. A message is associated with a User instance (so it is only
    applicable for registered users). There's no concept of expiration or
    timestamps. Messages are created by the Django admin after successful
    actions. For example, "The poll Foo was created successfully." is a
    message.
    """
    user = models.ForeignKey(User, related_name='_message_set')
    message = models.TextField(_('message'))

    def __unicode__(self):
        return self.message
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜