Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 754321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:59:55+00:00 2026-05-14T14:59:55+00:00

Does anyone know a clever way, in Google App Engine, to return a wrapped

  • 0

Does anyone know a clever way, in Google App Engine, to return a wrapped Model instance that only exposes a few of the original properties, and does not allow saving the instance back to the datastore?

I’m not looking for ways of actually enforcing these rules, obviously it’ll still be possible to change the instance by digging through its __dict__ etc. I just want a way to avoid accidental exposure/changing of data.

My initial thought was to do this (I want to do this for a public version of a User model):

class PublicUser(db.Model):
    display_name = db.StringProperty()

    @classmethod
    def kind(cls):
        return 'User'

    def put(self):
        raise SomeError()

Unfortunately, GAE maps the kind to a class early on, so if I do PublicUser.get_by_id(1) I will actually get a User instance back, not a PublicUser instance.

Also, the idea is that it should at least appear to be a Model instance so that I can pass it around to code that does not know about the fact that it is a "dumbed-down" version. Ultimately I want to do this so that I can use my generic data exposure functions on the read-only version, so that they only expose public information about the user.


Update

I went with icio’s solution. Here’s the code I wrote for copying the properties from the User instance over to a PublicUser instance:

class User(db.Model):
    # ...
    # code
    # ...

    def as_public(self):
        """Returns a PublicUser version of this object.
        
        """
        props = self.properties()

        pu = PublicUser()
        for prop in pu.properties().values():
            # Only copy properties that exist for both the PublicUser model and
            # the User model.
            if prop.name in props:
                # This line of code sets the property of the PublicUser
                # instance to the value of the same property on the User
                # instance.
                prop.__set__(pu, props[prop.name].__get__(self, type(self)))

        return pu

Please comment if this isn’t a good way of doing it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T14:59:56+00:00Added an answer on May 14, 2026 at 2:59 pm

    Could you not create a method within your User class which instantiates a ReadOnlyUser object and copies the values of member variables over as appropriate? Your call would be something like User.get_by_id(1).readonly() with the readonly method defined in the following form:

    class User(db.Model):
        def readonly(self):
            return ReadOnlyUser(self.name, self.id);
    

    Or you could perhaps have your User class extend another class with methods to do this automatically based on some static vars listing properties to copy over, or something.

    P.S. I don’t code in Python

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 375k
  • Answers 375k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use a custom validator. Using the two-parameter version,… May 14, 2026 at 8:21 pm
  • Editorial Team
    Editorial Team added an answer The string INTERVAL '1' day in your original query is… May 14, 2026 at 8:21 pm
  • Editorial Team
    Editorial Team added an answer All modern browsers will understand the Relative Reference URI format,… May 14, 2026 at 8:21 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.