Django - How Updating Author Model in the mid-way? - django

I have Author model which looks like this:
class Author(models.Model):
name = models.CharField(max_length=100)
email = models.EmailField(unique=True)
created = models.DateTimeField(auto_now_add=True)
last_login = models.DateTimeField(auto_now=True)
Now I want to connect Author Model to User model and in the process add some additional fields in the Author model. Here is the updated version of Author model.
class Author(models.Model):
user = models.OneToOneField(User)
## additional fields
phone = models.IntegerField(blank=True)
address = models.CharField(max_length=200)
bio = models.TextField()
Author model is also connected to the Story model via ForeignKey. My question is how do I update my Author model without deleting any Story related to it.

class Author(models.Model):
user = models.OneToOneField(User, null=True, blank=True)
And do makemigrations and migrate. Now you have the empty user field. Later you can add user in admin page or Can able to add user also in Shell.

Related

Django Rest Framework authentication with Custom User Model

I have multiple types of user in my django app: Employee and Patient. They have fields that are specific to each of them. They are implemented using the AbstractBaseUser model as below:
from django.db import models
from django.contrib.auth.models import AbstractBaseUser
class User(AbstractBaseUser):
username = models.CharField(max_length=40, unique=True)
USERNAME_FIELD = 'identifier'
first_name = models.CharField(
max_length=50, null=False, blank=False)
last_name = models.CharField(
max_length=50, null=False, blank=False)
date_of_birth = models.DateField(null=False, blank=False)
USER_TYPE_CHOICES = (
(1, 'Patient'),
(2, 'Employee'),
)
user_type = models.PositiveSmallIntegerField(
choices=USER_TYPE_CHOICES, default=1, blank=False, null=False)
class Role(models.Model):
RoleName = models.CharField(max_length=50, null=False, blank=False)
class Employee(models.Model):
user = models.OneToOneField(
User, on_delete=models.CASCADE, primary_key=True)
employment_start_date = models.DateField(null=False, blank=True)
employment_end_date = models.DateField(null=False, blank=True)
role = models.ForeignKey(
Role, on_delete=models.CASCADE, related_name='assigned_employees')
class Patient(models.Model):
user = models.OneToOneField(
User, on_delete=models.CASCADE, primary_key=True)
I have a few questions with how to go forward with this:
How does just the choice in the User class limit the fields that a user has access to? If I had a HTML page would I create an Employee then a User would be created, or the other way round?
When I'm using Django Rest Framework, how can I implement a sign up and log in with the two different types?
I'm struggling to understand how this would work conceptually. Is like Employee and Patient a subclass of User? Or are they separate models? Any help or advice would be greatly appreciated
In your code you don't have two types of User. You have only one type - class User(AbstractBaseUser). Employee and Patient are normal models that are only related to User.
If you wanted to create two types of User with actual inheritence, then you should do following:
class AbstractUser(AbstractBaseUser):
class Meta:
abstract = True
# main user fields here
class Employee(AbstractUser):
# employee fields here
class Patient(AbstractUser):
# patient fields here
If you don't want to do this, your current approach is good. You can simply authenticate User in standard way. During creation you can make seperate forms for registering employee User, that creates automatically related Employee class. Similar for Patient. They will share only fields of User class with either approach.
To authenticate in different ways you can use custom authentication with authenticate() function. Read specifics in Django Docs

Inheritance from an AbstractUser based Model in django 2.7

I have a Custom User Model extending the AbstractUser and it works fine. But now i want to create another Model that is extending my Custom User Model like this in models.py:
class CustomUser(AbstractUser):
phone = models.CharField(max_length=10, null=False, unique=True)
town = models.CharField(max_length=25, null=False)
class DeleveryPerson(CustomUser):
code = models.CharField(max_length=10, null=False, unique=True)
The problem is that the table DeleveryPerson is created with just the field "code"
when I expected it to also have fields coming from the CustomUser model.
So how can i achieve that kind of inheretane between CustomUser and DeleveryPerson. Thk!

How do I link the is_org_admin field in the User model to the org_admin field in the Organization model in Django?

I'm trying to create some models in Django. I currently have the following: User and Organization. One of the fields in the User model is "is_org_admin", which is a boolean. How should I link this field to the "org_admin" field in the Organization model? Here's the simplified code:
class Organization(models.Model):
id = ...
org_admin = models.OneToOneField("users.User"...
class User(AbstractUser):
id = ...
is_org_admin = models.BooleanField(default=False)
organization = models.ForeignKey("organizations.Organization", on_delete=models.CASCADE, null=True)
Make separate model for the Organization related details. Do not enforce it into User Model. You can do something like this:
class Organization(models.Model):
id = ...
name = models.CharField(max_length=128)
class Employee(models.Model):
id = ...
organization = models.ForeignKey(Organization, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
is_org_admin = models.BooleanField(default=False)
class User(AbstractUser):
id = ...
name = models.CharField(max_length=128)

Obtaining queryset based on unrelated models

I've got the following models. I need to obtain a queryset of orders where the user's userprofile.setupstatus == 1. Is this possible or should I just add a foreign key field on the Order model to the UserProfile?
class Order(models.Model):
user = models.ForeignKey(UserCheckout, null=True, on_delete=models.CASCADE)
class UserCheckout(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True, blank=True)
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
setupstatus = models.IntegerField(default=0)
It is surely possible with Django ORM
Your query should look somewhat like this
Order.objects.filter(user__user__userprofile__setupstatus=1)

Do I need a many-to-many relationship for the leave approver of a company?

I have a: User and Company
class User(AbstractBaseUser):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
company = models.ForeignKey(
'projects.Company',
on_delete=models.PROTECT
)
class Company(models.Model):
'''Company model
every user needs to be assigned to a company
'''
name = models.CharField(max_length=255, unique=True)
Now I need a to set a few leave approvers per company which will be Users. There can be a few and I want to set a priority for them.
If it were a single LeaveApprover then I would simply add a one-to-one to the Company model with a foreign key to LeaveApprover. But In my case A company can have many approvers, an approver can only approve a single company.
Do I need a many-to-many field?
I think adding a designation field in User model will work.
You can easily filter user based on designation ; It will differentiate if he is employee or approver.
The model will look like this....
class User(AbstractBaseUser):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
role = models.CharField(max_length=15)
company = models.ForeignKey(
'projects.Company',
on_delete=models.PROTECT
)
class Company(models.Model):
'''Company model
every user needs to be assigned to a company
'''
name = models.CharField(max_length=255, unique=True)
Now company can have many approvers and an approver can only approve a single company.
In your views you can write logic or assign specific permissions based on role.