Can I link my clients model to Django user models?
Hi new, using django and confusing the model part a bit for my project.
I need to create a site where the user can have their profile and make a post about the product they want to offer as a directory and I think I am creating these models:
class Users_Types(models.Model):
user_type = models.CharField(max_length=1)
def __str__(self):
return self.user_type
return self.id_type_user
class Users(models.Model):
users_types= models.OneToOneField('Users_Types')
e_mail = models.EmailField(unique=True)
password = models.TextField()
salt = models.TextField()
def __str__(self):
return self.id_user
return self.id_type_user
return self.e_mail
return self.password
return self.salt
class Clients(models.Model):
users = models.OneToOneField('Users')
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
ci = models.CharField(max_length=9)
birth_date = models.DateField(null=True, blank=True)
phone = models.CharField(max_length=20)
city = models.CharField(max_length=20, default=)
class products(models.Model):
clients = models.ForeignKey('clients')
product = models.CharField(max_length=30)
description = models.CharField(max_length=30)
Category = models.CharField(max_length=30)
but i read about django, i have a user model, i had to use that model and not my user model?
and how can I bind django user model to my clients model.
I appreciate your help!
source to share
Django does have a model User
, and all of this default authentication is done with that model, and from the get go, I highly recommend you keep this Django model User
as the default.
And in order to bind your client to the default user model, you need
from django.conf import settings
from django.db import models
class Client(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL)
...
Reading the django docs on Using the Django Authentication System would be very helpful for you.
I think you know what PEP-8 is and django has the same pep: Coding style , this will help you make your code much cleaner.
Also in the Django docs there is a paragraph on user model references : User model reference
source to share
Yes Django has its own custom module. Just import users into the models file first
from django.contrib.auth.models import User
class UserProfile(models.Model):
users_types= models.ForeignKey(User)
e_mail = models.EmailField(unique=True)
password = models.TextField()
class Clients(models.Model):
user = models.ForeignKey(User)
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
ci = models.CharField(max_length=9)
birth_date = models.DateField(null=True, blank=True)
phone = models.CharField(max_length=20)
city = models.CharField(max_length=20, default=)
You can get user
user = User.objects.get(id=request.user.id)
request.user contains the logged in user. You can also get customer information at
client_obj = Clients.objects.get(user=request.user)
thank
source to share