Sunday, 15 January 2012

How to set a Django Boolean value to True when by default, it is false? -


I have a demo view where users can sign up for any user who moves from this point forward, I would like to set the boolean value to true

  class person (models.model): user = models.OneToOneField (user, primary_key = true) facebook_id = models.charphield (max_length = 225, exclusive = true, empty = true, empty = True) twitter_id = models.CharField (max_length = 225, exclusive = true, empty = true, empty = true) suggest_person = model BooleanField (default = incorrect) userPic = models.CharField (max_length = 128, empty = true) phoneNum = models Signature Django view:  
  @csrf_exempt def signup (request): If request.method == 'POST': # Paste the JSON data = json.loads (request.raw_post_data) # Check that the user If the user already exists or does not exist then user.objects.filter (user name = data ['user name']) exists (): return HttpResponse ("user name already exists") # if not new user supplied With the information: newUser = User () newUser user name = data ['username'] newUser.email = data ['email'] newUser.set_pas sword (data ['password']) newUser.first_name = data [ 'First name'] newUser.last_name = data ['last name'] newUser.save () newPerson = personality (user = new user) 'PhoneNew', UserPic = data ['userPic'], userCover = data ['userCover']) if the data 'token': if person.objects.filter (token = data ['token ']) Is present (): oldToken = person.objects.get (token = data [' token ']) old token Token = None oldToken.save () newPerson.token = Data ['token'] newPerson.save () Other: newPerson.token = data ['token'] NewPerson.save () # Like it? NewPerson.suggested_person = true newPerson.save () newUser.relationships.add (newUser) return HttpResponse (str (newUser.id)) # if there is no post: return HTTPPPction ("people register here")  < / Pre> 

How can I go about creating a user who signs up from this point, which is a 'suggested_person'?

If you can not change the default definition on your model or not, then you can either You can pass in the desired value when your person makes an example:

  newPerson = person User = newUser, phoneNum = data ['phoneNum'], userPic = data ['userPic'], userCover = Data ['userCover'], suggest_person = true) or you can set attribute before saving:  
  newPerson.suggested_person = true < / Code>   

No comments:

Post a Comment