Django profile not saving - django-models

this had keeped me bussy for some time, I hope you can help me.
Context: UserA and UserB are related on some way. In the updateview for the profile of UserA, I also need to make some changes on the profile of UserB but, altough the view runs without errors, the changes on UserB's profile are not saved (changes on UserA's profile do are saved).
My models are something like this:
class UserProfile(models.Model):
... fields ...
class Meta:
abstract = True
class ManProfile(UserProfile):
some_flag = models.BooleanField(default=False)
... more fields ....
class WomanProfile(UserProfile):
some_flag = models.BooleanField(default=False)
... more fields ....
def get_profile(user):
if user.user_type == 'm':
obj, created = ManProfile.objects.get_or_create(user=user)
return obj
elif user.user_type == 'f':
obj, created = WomanProfile.objects.get_or_create(user=user)
return obj
else:
return None
CustomUser.profile = property(lambda u: get_profile(u))
Before the view runs both users are like this:
userA.profile.some_flag = False
userB.profile.some_flag = False
And the view is something like this:
class ProfileUpdate(UpdateView):
... stuff ...
def form_valid(self, form):
userB = CustomUser.objects.get(username=self.request.user.profile.friend_username)
userB.profile.some_flag = True
userB.profile.save()
form.instance.some_flag = True
return super(ProfileUpdate, self).form_valid(form)
After the view runs, the results are:
userA.profile.some_flag = True
userB.profile.some_flag = False
UserA's profile is saved (which is handled by the form) but UserB's profile is not (although I'm calling userB.profile.save()).
If I change the view to something like this both profiles are saved:
class ProfileUpdate(UpdateView):
... stuff ...
def form_valid(self, form):
userB = CustomUser.objects.get(username=self.request.user.profile.friend_username)
userB.manprofile.some_flag = True
userB.manprofile.save()
form.instance.some_flag = True
return super(ProfileUpdate, self).form_valid(form)
I stopped using the lambda function and instead I called the profile name directly (manprofile in this case because both users are male) and then all is working fine. So, my question is if is possible to call save() on the lambda function and if it is, what is wrong with my code.

The problem was that the profile called by the lambda function was not cached, so after the asigment, when I called save on the instance, I was actually calling save on a new clean instance of the profile.

Related

fetch data from ndb query by KeyProperty field

I have User model and another model is user token
class User(EndpointsModel):
name = ndb.StringProperty()
#classmethod
def get_by_name(cls,name)
return cls.query(cls.name == name).get()
2 model is user token
class Token(EndpointsModel):
user = ndb.KeyProperty()
token = ndb.StringProperty()
#classmethod
def get_by_user(cls, key):
return cls.query(cls.token == key).get()
Now i fetch data from user
data = User.get_by_name('jaskaran')
when i try to fetch token then it return me None
print( Token.get_by_user(data.key) )
this is return None How can i fetch data from token with the help of user keyProrperty?
I think that you may be looking for ancestor queries, that according to the official documentation, look like this:
As the example in the docs shows:
purchases = Purchase.query(
Purchase.customer == customer_entity.key).fetch()
This applied to your use case will be then something like:
class Token(EndpointsModel):
user = ndb.KeyProperty(kind=User) #added kind User in key property, just in case
token = ndb.StringProperty()
#classmethod
def get_by_user(cls, key):
return cls.query(cls.token == key).fetch()
###Fetching data
data = User.get_by_name('jaskaran')
##HERE WILL BE GOOD TO PRINT DATA.KEY, FOR EXAMPLE,
##TO MAKE SURE THAT YOU ARE ACTUALLY RETRIEVING SOMETHING
print(data.key)
print(Token.get_by_user(data.key)
As a side note, the docs also explain that Account.get_by_id(...) is faster than Account.query(...).get(), therefore removing the .get() will be a good update to the code.

Simple datastore entity with 2 fields that are also unique

All I am trying to produce is an entity that holds a unique username, and a unique device ID, and the ability to return an error if either of these conditions are not met on submission.
The only way I can see is to perform a query within a transaction, then filter the results. This however requires an ancestor (which seems unnecessary for a single simple entity).
What is the best method to go about doing this?
Here is an example that does what you want.
I put 2 entities to show you also how to make relationships
class Person(ndb.Expando):
registration_date = ndb.DateTimeProperty(auto_now_add=True)
#property
def info(self):
info = PersonInfo.query(ancestor=self.key).get()
return info
class PersonInfo(ndb.Expando):
email = ndb.StringProperty()
nick_name = ndb.StringProperty()
edit_date = ndb.DateTimeProperty(auto_now=True)
Later in the controller for register:
class RegisterPersonHandler(webapp2.RequestHandler):
def get(self):
user = users.get_current_user() #Stub here
if not user:
self.redirect(users.create_login_url(self.request.uri), abort=True)
return
person = Person.get_or_insert(user.user_id())
if not self._register(person, user):
# more logging is needed
logging.warning('Warning registration failed')
return
#ndb.transactional()
def _register(self, person, user):
''' Registration process happens here
'''
# check if the person has info and if not create it
info = PersonInfo.query(ancestor=person.key).get()
if not info:
info = PersonInfo(id=user.user_id(), parent=person.key)
info.nick_name = user.nickname()
info.email = user.email()
info.put()
return True
To answer also the comment question:
How can you programatically tell whether the returned entity is a new
or existing one though?
Try checking against a property that is default. Eg creation_date etc.
Though you can also check on something you need or on another entity's existence like I do because I expect the data to be consistent, and if not then create the bond.

How to structure movies database and user choices?

I would like to create movies database, where user will be able to mark movies he/she watched and liked:
class Movies(ndb.Model):
watched = ndb.UserProperty()
liked = ndb.UserProperty()
Will that work? I use Google accounts.
How should I choose later all movies user liked?
Upd. I've followed systempuntoout approach and use the following code to save user choices:
user = users.get_current_user()
if user:
userschoices = models.UsersChoices(
movie=ndb.Key(models.Movies, movie_id), # TODO: what if movie_id is wrong?
watched=True,
user_id=user.user_id()
)
try:
userschoices.put()
self.response.out.write('1')
except:
self.response.out.write('0')
But if user makes his choice several times, then several records are added to the datastore...
Wouldn't be it better just to save user id and movie id as keyname?
userschoices = models.UsersChoices.get_by_id(user.user_id() + '-' + movie_id)
if userschoices is None:
userschoices = models.UsersChoices(id=user.user_id() + '-' + movie_id)
userschoices.movie = ndb.Key(models.Movies, movie_id) # TODO: what if movie_id is wrong?
userschoices.user_id = user.user_id()
if option == 'liked':
userschoices.liked = True
elif option == 'watched':
userschoices.watched = True
However, with such approach if I don't pass liked, then it overwrites its value with None (the same with watched, if not passed, None is used).
I would go with two different Models, one that stores all the Movies details and one to store the UserChoices :
class Movies(ndb.Model):
title = ndb.StringProperty(required=True)
director = ndb.StringProperty()
whatever = ndb.StringProperty()
class UsersChoices(ndb.Model):
movie = ndb.KeyProperty(kind=Movies, required=True)
watched = ndb.BooleanProperty(required=True)
liked = ndb.BooleanProperty(required=True)
user_id = ndb.StringProperty(required=True)
#classmethod
def get_liked_movies(cls, user_id):
return cls.query(cls.user_id == user_id, cls.liked == true).fetch(10)
#classmethod
def get_watched_movies(cls, user_id):
return cls.query(cls.user_id == user_id, cls.watched == true).fetch(10)
#classmethod
def get_by(cls, user_id, movie_key):
return cls.query(cls.user_id == user_id, cls.movie == movie_key).get()
If you need to store informations about users you should create your UserInfo Model, keyed by user_id from the users API, with all the details Properties your application needs.
class UserInfo(ndb.Model):
#Keyed by user_id
nickname = ndb.StringProperty()
email = ndb.StringProperty()
To create a new UserInfo, you could do:
from google.appengine.api import users
user = users.get_current_user()
userinfo = UserInfo(
id = user.user_id(),
nickname = user.keyname(),
email = user.email()
)
userinfo.put()
Then, when the user is logged in, use his/her user_id to retrieve the watched/liked movies.
from google.appengine.api import users
user = users.get_current_user()
userinfo = ndb.Key(UserInfo, user.user_id()).get()
watched_movies = UsersChoices.get_watched_movies(userinfo.key.id())
liked_movies = UsersChoices.get_liked_movies(userinfo.key.id())
It appears you are trying to model a many-to-many relationship. There are a few ways to model this relationship (see the Many-to-Many section). See also Nick's blog. (Unfortunately, neither of those references are written for NDB, so, for example, you can't use collection_name, i.e., back-references. But they are still useful in showing you how to break up the data into different models.)
Here's one way you could do it, using "join tables"/"relationship models":
class Movie(ndb.Model):
title = ndb.StringProperty(required=True)
class LikedMovie(ndb.Model):
movie = ndb.KeyProperty(kind=Movie, required=True)
user = ndb.StringProperty(required=True) # user.user_id()
class WatchedMovie(ndb.Model):
movie = ndb.KeyProperty(kind=Movie, required=True)
user = ndb.StringProperty(required=True) # user.user_id()
...
movies_user_likes = LikedMovie.query(LikedMovie.user == user.user_id()).fetch()
Depending on how many users your application will support, and how often the database will be updated, it may be more efficient to use repeated properties (i.e., lists of users) instead of join tables:
class Movie(ndb.Model):
title = ndb.StringProperty(required=True)
users_who_watched = ndb.StringProperty(repeated=True) # list of user.user_id()s
users_who_liked = ndb.StringProperty(repeated=True) # list of user.user_id()s
...
movies_user_likes = Movie.query(Movie.users_who_liked == user.user_id()).fetch(projection=[Movie.title])
Note that I used a projection query above, so that the users_who_watched lists are not returned with the query results. You probably don't need these, and this should make fetching significantly faster.
If you expect, say, less than 1,000 users to watch or like a particular movie, the list approach might be better.
For a more advanced technique, see Building Scalable, Complex Apps on App Engine, where Brett shows how to move the repeated/list property into a separate model, using parent keys.

django i can't save form with 2 foreign keys

i have a Reply class:
class Reply(models.Model):
reply_to = models.ForeignKey(New)
creator = models.ForeignKey(User)
reply = models.CharField(max_length=140,blank=False)
a replay form:
class ReplyForm(ModelForm):
class Meta:
model = Reply
fields = ['reply']
where New is the Post class (containing users posts)
and a view
def save_reply(request):
#u = New.objects.get(pk=id)
if request.method == 'POST':
form = ReplyForm(request.POST)
if form.is_valid():
new_obj = form.save(commit=False)
new_obj.creator = request.user
new_obj.reply_to = form.reply_to
# reply_to_id = u
new_post = New(2) #this works hardcoded, but how can i get the blog New post #id, as a parameter, instead?
new_obj.reply_to = new_post
new_obj.save()
return HttpResponseRedirect('.')
else:
form = ReplyForm()
return render_to_response('replies/replies.html', {
'form': form,
},
context_instance=RequestContext(request))
where created_by belongs to New class and represents the creator of the post (which is to be replied)
how can i assign the current post to the reply under it?
thanks in advance!
I may have missed something, but reply_to needs an instance of the New model. New.id doesn't look like one to me?
new_obj.reply_to = New.id
Do you have an instance of the New model available at that point that you can assign?
ah, I see you've tweaked the question
If you don't have an instance of the New model, you'll need to create one
new_post = New(whatever, goes, here)
new_post.save()
Then assign it to reply_to
new_obj.reply_to = new_post
Or similar.
edit
Without knowing exactly that ReplyForm looks like I'm guessing a bit, but presumably it's based on the Reply object, letting the user select the reply_to field somehow or other?
Assuming that the form's reply_to variable is populated & correct I think you should just be able to do:
form = ReplyForm(request.POST)
if form.is_valid():
new_obj = form.save(commit=False)
new_obj.creator = request.user
new_obj.reply_to = form.reply_to
new_obj.save()
In fact since it's a foreign key, the new_obj = form.save(commit=False) may have already set .reply_to for you? The Django Model Forms docs may help.
Not sure if this'll be appropriate for your app or not, but you could try making use of a form widget, in particular the HiddenInput one to include the post (id) in the form. Something like
class ReplyForm(ModelForm):
reply_to = forms.ModelChoiceField(New.objects.all(), widget=forms.HiddenField)
class Meta:
model = Reply
fields = ['reply', 'reply_to']
widgets = {
'reply_to': HiddenField,
}
(Not sure that's entirely correct but see overriding-the-default-field-types-or-widgets for more).
You've now enabled the id you need to be passed to the client and back through the form, you now just need to put it in when you create the form for display in the first place
else:
form = ReplyForm()
form.reply_to = # ... fill in the current post (New inst) being replied to
# presumably from somewhere in the request object?
return render_to_response('replies/replies.html', { 'form': form, },
Hopefully that doesn't lead you off on the wrong track - completely untested, E&OE, YMMV, etc, etc

How to do custom display and auto-select in django admin multi-select field?

I'm new to django, so please feel free to tell me if I'm doing this incorrectly. I am trying to create a django ordering system. My order model:
class Order(models.Model):
ordered_by = models.ForeignKey(User, limit_choices_to = {'groups__name': "Managers", 'is_active': 1})
in my admin ANY user can enter an order, but ordered_by must be someone in the group "managers" (this is the behavior I want).
Now, if the logged in user happens to be a manager I want it to automatically fill in the field with that logged in user. I have accomplished this by:
class OrderAdmin(admin.ModelAdmin):
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "ordered_by":
if request.user in User.objects.filter(groups__name='Managers', is_active=1):
kwargs["initial"] = request.user.id
kwargs["empty_label"] = "-------------"
return db_field.formfield(**kwargs)
return super(OrderAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
This also works, but the admin puts the username as the display for the select box by default. It would be nice to have the user's real name listed. I was able to do it with this:
class UserModelMultipleChoiceField(forms.ModelMultipleChoiceField):
def label_from_instance(self, obj):
return obj.first_name + " " + obj.last_name
class OrderForm(forms.ModelForm):
ordered_by = UserModelChoiceField(queryset=User.objects.all().filter(groups__name='Managers', is_active=1))
class OrderAdmin(admin.ModelAdmin):
form = OrderForm
My problem: I can't to both of these. If I put in the formfield_for_foreignkey function and add form = OrderForm to use my custom "UserModelChoiceField", it puts the nice name display but it won't select the currently logged in user. I'm new to this, but my guess is that when I use UserModelChoiceField it "erases" the info passed in via formfield_for_foreignkey. Do I need to use the super() function somehow to pass on this info? or something completely different?
Eliminate the ModelChoiceField/ModelMultipleChoiceField subclass completely and work off the formfield_for_foreignkey method. The request argument isn't available in the subclass, and so you can't get the current user.
Then use label_from_instance method inside formfield_for_foreignkey. You can write this yourself, but a robust Django snippet is available at http://djangosnippets.org/snippets/1642/. Just subclass the class from that snippet. You can put it in a different file and import it, or just write it above the OrderAdmin class as OrderAdmin(NiceUserModelAdmin).
Lastly, rewrite the formfield_for_foreignkey method to take the kwargs["initial"] = request.user.id outside the if statement. I don't think that's necessary and I too had trouble making it work that way.
# admin.py
from django.contrib import admin
from django.contrib.auth.models import User
from (...) import Order
class NiceUserModelAdmin(admin.ModelAdmin):
# ...
class OrderAdmin(NiceUserModelAdmin):
# ...
def formfield_for_foreignkey(self, db_field, request, **kwargs):
kwargs["initial"] = request.user.id
if db_field.name == "ordered_by":
kwargs["empty_label"] = "-------------"
return db_field.formfield(**kwargs)
return super(OrderAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)

Resources