Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I link my clients model with Django User models?

Hi im new using django and is a little confusing the part of model for my project now.

i need to create a site that user can have their profile and make post of product that they want to offer like a catalog and i'm thinking create this 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 have it user model, i had to use that model instead that my Users model?

and how i can link the django user model with my clients model.

i appreciate your help!

like image 352
Steven Daniel Anderson Avatar asked Feb 05 '23 08:02

Steven Daniel Anderson


2 Answers

Django indeed has it's User model and all default authentication is made through that model, and from the start i would strongly recommend you to keep for that default Django User model.

And for linking your Client with default user model you need to

from django.conf import settings
from django.db import models

class Client(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL)
    ...

Reading django docs on Using the Django authentication system would be very much useful for you.

I think that you know what PEP-8 is, and django has it's kinda same pep: Coding style, it would help you make your code much cleaner.

Also Django docs has paragraph about referencing to User model: Referencing the User model

like image 82
vishes_shell Avatar answered Feb 06 '23 22:02

vishes_shell


Yes Django has it's own User built in module. Just import Users first in your models file

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 by

    user = User.objects.get(id=request.user.id)

request.user is containing your logged in user. You can also get you client info by

    client_obj = Clients.objects.get(user=request.user)

Thank you

like image 38
Sagar Avatar answered Feb 06 '23 21:02

Sagar