Niklas Rosencrantz
Niklas Rosencrantz

Reputation: 26652

How to make references between expando models?

Update

This was my best effort creating the following scheme enter image description here

user = self.auth.store.user_model.create_user(email,
        password_raw=newpasswd)
if not user[0]:  # user is a tuple
    return user[1]  # Error message
else:

    # User is created, let's try making the references
    okuser = auth_models.User.get_by_id(long(user[1].key.id()))
    okuser.sponsor = auth_models.User.get_by_id(long(sponsor_id)).auth_ids

Original question

How can I make a selfreference with an expando class to indicate which User is the "sponsor" of which? The "sponsor" is the one who invited the new User so at creation we must store that and it would be much neater to store it as a referenceproperty than a string or a stringlist.

I can create a new user but I don't know how to make a reference so that I can tell for one User who another User is who is the sponsor of the first user and I suppose a way to model this is with selfreferenceproperty since both objects are users but the complication is that it is an expando model so I don't know how to use the reference property. Could you tell me how to do it or give me a clue how I can solve this problem in the best way?

user = self.auth.store.user_model.create_user(email,
        password_raw=newpasswd)
if not user[0]:  # user is a tuple
    return user[1]  # Error message
else:

    # User is created, let's try making the reference
    okuser = auth_models.User.get_by_id(user[1].key.id())
okuser.sponsor = db.SelfReferenceProperty(User,
    collection_name='matched_images', verbose_name='Sponsor')

I don't know how to do the last part, to store the actual referenceproperty with an epando model. How can it be done?

Update

It seems it can't be done:

NotImplementedError: Property sponsor does not support <class 'google.appengine.ext.db.ReferenceProperty'> types.

Code:

user = self.auth.store.user_model.create_user(email,
        password_raw=newpasswd)
if not user[0]:  # user is a tuple
    return user[1]  # Error message
else:

    # User is created, let's try redirecting to login page
    okuser = auth_models.User.get_by_id(long(user[1].key.id()))
    okuser.sponsor = db.SelfReferenceProperty(auth_models.User.get_by_id(sponsor_id),collection_name='matched_distributor')
    okuser.put()

It forces me do use a string instead of a reference and then a solution is feasible:

user = self.auth.store.user_model.create_user(email,
        password_raw=newpasswd)
if not user[0]:  # user is a tuple
    return user[1]  # Error message
else:

    # User is created, let's try redirecting to login page
    okuser = auth_models.User.get_by_id(long(user[1].key.id()))
    okuser.sponsor =  sponsor_id
    okuser.put()

Upvotes: 1

Views: 447

Answers (1)

Nick Johnson
Nick Johnson

Reputation: 101139

You can't assign an instance of a Property class to an instance of a model - property classes define properties, they don't represent individual values.

By far the easiest way to do what you want is to add the property as you would on a regular model. Just because you're using expandos (why, by the way?) doesn't mean you can't have regular properties on them as well.

Upvotes: 1

Related Questions