1

I have register page,the users register their data.the thing is I need to provide tokens to registered users while they are registering,for this I have User model,and I added rest_framework.authtoken in installed apps and DEFAULT_PERMSSION ,AUTH_USER_MODEL in settings.py, and I have owner field in User class, when I'm trying to register getting this error IntegrityError: (1048, "Column 'owner_id' cannot be null") .

views.py

def transregister(request):
    if request.POST:
        args = {}
        args.update(csrf(request))
        # try:

        if request.method == 'POST':

            user = UserForm(request.POST)
            username = request.POST['username']
            email = request.POST['email']
            password = request.POST['password']
            confirm_pwd = request.POST['confirm_pwd']
            company_name = request.POST['company']

            profile_type = request.POST['trainer-company']

            trainerprofile = TrainerForm(request.POST)

            corporate_profile = CorporateProfileForm(request.POST)
            print username


            if user.is_valid():
                userModel = user.save(commit=False)
                userModel.is_user = True
                userModel.is_active=True
                userModel.save()

models.py

class User(models.Model):
    username = models.CharField(max_length=150)
    #username = models.OneToOneField(User, on_delete=models.CASCADE)
    email = models.CharField(max_length=250,unique=True)
    company = models.CharField(max_length=250)
    password = models.CharField(max_length=150)
    confirm_pwd = models.CharField(max_length=150)
    is_admin = models.BooleanField(default=False)
    is_user = models.BooleanField(default=False)
    is_active = models.BooleanField(default=False)
    device_id = models.CharField(max_length=1000, blank=True, null=True)

  #  User = get_user_model()
    user = settings.AUTH_USER_MODEL
    owner = models.ForeignKey('user', related_name='User', on_delete=models.CASCADE)
    highlighted = models.TextField(default = '')

    objects = UserManager()

    #class Meta:
    #    db_table = u'index_user'


    REQUIRED_FIELDS= ('username',)
    USERNAME_FIELD = ('email')

    def __str__(self):
        return self.username

@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_auth_token(sender, instance=None, created=False, **kwargs):
    if created:
        Token.objects.create(user=instance)

serializers.py

class UserSerializer(serializers.ModelSerializer):
    owner = serializers.PrimaryKeyRelatedField(many=True, queryset=User.objects.all())
    #print 'owner'
    #owner = serializers.ReadOnlyField(source='owner.username')
    class Meta:
        model = User
        fields = ('id','username','email','company','password','confirm_pwd','is_admin','is_user','is_active','device_id','highlighted','owner')
2
  • Which table is it saying that is in? does it give the same error on migration? Commented Feb 8, 2017 at 0:48
  • i have inddex_user table,and no error at migrations time and i have DEFAULT_PERMISSION_CLASS,DEFAULT_AUTHENTICATION_CLASS and AUTH_USER_MODEL=index.USER in settings.py Commented Feb 8, 2017 at 4:30

1 Answer 1

1

In your settings.py you need to configure the authentication classes to include TokenAuthentication.

To set it in settings.py you should use this:

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
    ),
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.TokenAuthentication',
    )
}

Now since you're also creating your own user model, you need to privide a value for the AUTH_USER_MODEL setting that references your custom model:

AUTH_USER_MODEL = 'myapp.MyUser'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.