0

I started learning Django last week and I thought the best way is to just build an App. I'm stuck now with the User Auth module with my tests. Maybe someone could help me out: I want to model employees in the db and also put them in the User Auth model, so I build the model and after a few checks I want to add it to User.

class Employee(models.Model): session = models.ForeignKey( 'sessions.Session', verbose_name='Session', blank=True, null=True, ) email = models.CharField(max_length=255) password1 = forms.CharField(max_length=30, widget=forms.PasswordInput()) #render_value=False password2 = forms.CharField(max_length=30, widget=forms.PasswordInput()) first_name = models.CharField(max_length=255) last_name = models.CharField(max_length=255) region = models.ForeignKey(Region, on_delete=models.CASCADE) workgroup = models.ForeignKey(Workgroups, on_delete=models.CASCADE) user_since = models.DateTimeField('Signed up since', default=timezone.now()) rights = models.IntegerField(default=0) def __str__(self): return self.email def clean_username(self): # check if username dos not exist before try: User.objects.get(username=self.cleaned_data['email']) #get user from user model except User.DoesNotExist : return self.cleaned_data['email'] raise ValidationError("this user exist already") def clean(self): # check if password 1 and password2 match each other if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:#check if both pass first validation if self.cleaned_data['password1'] != self.cleaned_data['password2']: # check if they match each other raise ValidationError("passwords dont match each other") return self.cleaned_data def dosave(self): # create new user new_user=User.objects.create_user(self.cleaned_data['username'], self.cleaned_data['email'], self.cleaned_data['password1']) new_user.save() return new_user 

So, next I wrote a few tests, one of them as following:

myuser = Employee.objects.create(first_name='Annie', last_name='Levers', email='[email protected]', region=mymun, workgroup=mygroup, user_since=timezone.now()) self.assertEquals( str(myuser), '[email protected]', ) # Check user.auth db userpk = myuser.id user = get_object_or_404(User, pk=userpk) user.set_password('abcxyz123') user.save() 

When I run this test, the python cannot find an entry for userpk in User but gives me a 404 object.

Any ideas? Thanks so much already!

1 Answer 1

1

If you want to create a custom user model you should use AbstractBaseUser.

https://docs.djangoproject.com/es/1.9/topics/auth/customizing/#specifying-a-custom-user-model

Sign up to request clarification or add additional context in comments.

1 Comment

@Becher This. The model that is going to be used for auth should extend AbstractBaseUser, otherwise you are in for a lot of custom code and digging through source, and for no apparent gain in this case.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.