1

我有一个 django 视图,用户可以在其中注册。对于从现在开始注册的任何用户,我想将布尔值设置为 True。

 class Person(models.Model):
      user = models.OneToOneField(User, primary_key=True)
      facebook_id = models.CharField(max_length=225, unique=True, null=True, blank=True)
      twitter_id = models.CharField(max_length=225, unique=True, null=True, blank=True)
      suggested_person = models.BooleanField(default=False)
      userPic = models.CharField(max_length=128, blank=True)
      phoneNum = models.CharField(max_length=128, blank=True)

注册 Django 视图:

 @csrf_exempt
 def signup(request):

     if request.method == 'POST':
         # Parse the JSON
         data = json.loads(request.raw_post_data)
         # Check if the user already exists
         if User.objects.filter(username = data['username']).exists():
             return HttpResponse("Username already exists")

         # If not then create new user with the supplied info
         else:
             newUser = User()
             newUser.username = data['username']
             newUser.email = data['email']
             newUser.set_password(data['password'])
             newUser.first_name = data['firstName']
             newUser.last_name = data['lastName']
             newUser.save()

             newPerson = Person(user = newUser, phoneNum = data['phoneNum'], userPic = data['userPic'], userCover = data['userCover'])
             if 'token' in data:
                 if Person.objects.filter(token=data['token']).exists():
                     oldToken = Person.objects.get(token=data['token'])
                     oldToken.token = None
                     oldToken.save()
                     newPerson.token = data['token']
                     newPerson.save()
                 else:
                     newPerson.token = data['token']
                     newPerson.save()

     #perhaps like this? newPerson.suggested_person =True
             newPerson.save()
             newUser.relationships.add(newUser)

             return HttpResponse(str(newUser.id))

     # If not POST  
     else:
         return HttpResponse("Signup here People")

我怎样才能让任何从这一点开始注册的用户成为“推荐人”?

4

1 回答 1

2

如果您不能或不想更改模型的默认定义,则可以在创建 Person 实例时传入所需的值:

newPerson = Person(user = newUser, phoneNum = data['phoneNum'], userPic = data['userPic'], userCover = data['userCover'], suggested_person=True)

或者您可以在保存之前设置属性:

newPerson.suggested_person = True
于 2013-06-23T03:23:37.180 回答