Django - Creating a row in onther table before creating user - django

I have user, userprofile and company tables.
I want to create a record in company table and then assign the id of the newly created company in the userprofile foreign key before creating the user. I think it can be done using pre_save signal but I am unable to figure how. Please help.
Here's some details as AMG asked:
I have django's builtin user model
a userprofile model
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
image = models.ImageField(default='profile_pics/default.jpeg', upload_to='profile_pics')
bio = models.TextField(blank=True, verbose_name='Biography')
company_name = models.ForeignKey(Company, on_delete=models.CASCADE)
a company model
class Company(models.Model):
name = models.TextField(blank=True, verbose_name="Company Name")
nof = models.IntegerField(verbose_name="No. of Employees")
All three are related user and userprofile has one to one relationship, and userprofile and company has many to one relationship.
I want Company record to be created first and than that new record's reference needs to be put into userprofile.
I think I have made it clear now.

Exactly, you can use pre_save for this. You can just put this in models.py below your defined models.
from django.db.models.signals import pre_save
#receiver(pre_save, sender=Profile) # register your model to the signal pre_save
def my_callback(sender, instance, *args, **kwargs):
if not instance.company_name: # check if instance has a value for company_name
company = Company.objects.create(
name='Test',
nof=1
)
instance.company_name = company
OR
Create a default value through a function.
class Company(models.Model):
name = models.TextField(blank=True, null=True, verbose_name="Company Name") # set blank=true and null=true so you can save an empty instance
nof = models.IntegerField(blank=True, null=True, verbose_name="No. of Employees")
def profile_company_default():
return Company.objects.create()
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
image = models.ImageField(default='profile_pics/default.jpeg', upload_to='profile_pics')
bio = models.TextField(blank=True, verbose_name='Biography')
company_name = models.ForeignKey(Company, on_delete=models.CASCADE, default=profile_company_default) # add function as default value
EDIT
To edit company after save.
profile = Profile.objects.create(key="value")
profile.company.name = "Company Name"
profile.company.nof = 5
profile.save()

Related

How to get email from usercusomer for followers

I want to send an email to each follower after creating the item. But I don't know how to get this field from Models, and also I don't know how to send each follower to relate following.
models.py
class Book(models.Models):
title = models.CharField(max_length=255)
author = models.ForeignKey(
"users.CustomUser", on_delete=models.SET_NULL, null=True
)
# This is my try
#But it doesn't work
#hook(AFTER_CREATE)
def send_followers(self):
user = Followers.objects.all()
followers_email = CustomUser.objects.filter(
followers__follower__in=, followers__following=self.author.profile)
if CustomUser.objects.filter(
followers__follower__in=CustomUser.objects.only(
'followers__follower'),
followers__following=self.author.profile
).exists():
send_mail(
"Article published",
'%s' % (
followers_email
),
"nikitaalexnazarov#yandex.ru",
[self.author.email],
fail_silently=False,
)
else:
pass
class CustomUser(AbstractUser):
gender = models.ForeignKey(
"Gender", on_delete=models.CASCADE, blank=True, null=True
)
class Followers(models.Model):
follower = models.ForeignKey(
"users.CustomUser", on_delete=models.CASCADE, null=True, blank=True)
following = models.ForeignKey(
"Profile", on_delete=models.CASCADE, null=True, blank=True)
class Profile(models.Model):
slug = models.SlugField(unique=True)
user = models.OneToOneField(get_user_model(), on_delete=models.CASCADE)
Your requirement can be fulfilled with signals. In Django, signals are used to perform certain actions based on some events. In your case, you want to send emails to all the followers of the author whenever a new book is created. Code that sends mails can be implemented in receivers. post_save signal will be triggered whenever a new row is created in the Book model. The corresponding signal will be received by the 'send_mail' receiver and will be executed.
signals.py
from django.dispatch import receiver
#reciever(post_save, sender="Book")
def send_mail(sender, instance, **kwargs):
# get the user model row of the author. Since author field is in M2One relationship with CustomUser, we can access the id of the user.
user_obj = instance.author
# ProFile model is in One2One relationship with CustomUser. Get the profile instance/row corresponding to the user_obj.
profile_obj = user_obj.profile
followers_list = Followers.objects.filter(following=profile_obj)
#followers_list will be having a list of all the followers to whom mail can be sent. You can retrieve the email of the followers present in followers_list.
apps.py
from django.apps import AppConfig
class AppNameConfig(AppConfig):
name = 'AppName'
def ready(self):
import signals over here

Why does Django not find a field added to the AbstractBaseUser

I've inherited from the AbstractBaseUser as follows:
class User(AbstractBaseUser):
"""
Main User model, inherits from AbstractBaseUser
"""
# Meta
email = models.EmailField(verbose_name='email', max_length=60, unique=True)
username = models.CharField(max_length=40, unique=True) # equals to email
date_joined = models.DateTimeField(verbose_name='date joined', auto_now_add=True)
last_login = models.DateTimeField(verbose_name='last login', auto_now=True)
employee_of = models.OneToOneField(Customer, on_delete=models.SET_NULL, null=True)
So each User is linked to one and only one Customer.
Now within a view I want to access the instance of the current logged in user within the request object and get the employee_of value to get a queryset that contains all users of that customer.
def render_employees(request):
"""
Renders the employees page of the dashboard
:param request:
:return:
"""
# Return the value for the current site for css specific classes
dashboard_site = 'employees'
# Query the employees
qs_employees = User.objects.filter(employee_of=request.user.employee_of) # doesn't find field
...
However the filter doesn't work because request.user.employ_of doesn't seem to return anything. My IDE even suggests e.g. username, date_joined etc. but not employee_of.
Why's that?
class Customer(models.Model):
"""
A table that stores static data about a customer, usually a legal company
"""
legal_name = models.CharField(max_length=50)
street = models.CharField(max_length=30)
street_number = models.CharField(max_length=3)
def __str__(self):
return self.legal_name
Update:
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
from applications.customer.models import Customer
from django.conf import settings
BaseUser = settings.AUTH_USER_MODEL
class User(AbstractBaseUser):
"""
Main User model, inherits from AbstractBaseUser
"""
# Relations
user = models.OneToOneField(BaseUser, related_name='user_profile', on_delete=models.CASCADE, null=True) # link to default user model
employee_of = models.OneToOneField(Customer, on_delete=models.SET_NULL, null=True, blank=True)
I linked the user to the default user model via Django admin. However in the view im still not able to access employee_of within request.user
It seems that request.user is a different model. It's User model from django.contrib.auth. https://docs.djangoproject.com/en/4.0/ref/contrib/auth/#django.contrib.auth.models.User.
What you can do about it?
In our app we have UserProfile model that have OnetoOne relation to django User.
You can then store employee_of value there.
class UserProfile(AbstractBaseUser):
user = models.OnetoOneField("auth.User", related_name="user_profile", on_delete=models.CASCADE)
employee_of = models.OneToOneField(Customer, on_delete=models.SET_NULL, null=True)
and then access request.user employees using something like
request.user.user_profile.employee_of

How to automatically create an object for a model in Django, if it doesn't exist?

I have two models. One is for UserProfile and the other is for Company.
class UserProfile(models.Model):
company_name = models.ForeignKey(Company, on_delete = models.CASCADE, related_name = 'company')
class Company(models.Model):
name = models.CharField(max_length = 100)
I am using Django Rest Framework for user creation. What I want to achieve is when I create a new user, I want to assign a company_name to that user. And if that company_name is not present in the db, then I want to create it on the go. But it is throwing an error. "Invalid hyperlink - No URL match."
You can use python's #property to tackle this problem in a clean and simple way. It works well for creating and for updating the object aswell. Note that the UserPorifle's field is renamed to company. Here is how you do it:
class Company(models.Model):
name = models.CharField(max_length=100)
class UserProfile(models.Model):
company = models.ForeignKey(Company, on_delete=models.CASCADE, related_name='company')
#property
def company_name(self):
return self.company.name
#company_name.setter
def company_name(self, value):
self.company, _ = Company.objects.get_or_create(name=value)
Now you can create objects using:
UserProfile.objects.create(company_name='Some name')
First you need to link your UserProfile Model with the user. It should be a OnetoOne Relationship because a User should only have one company I guess.
In your serializer you should add in the Company model and save the company name from the input in the API and then connect it to the new user that is being created.

Can't use the extended User model

I want to add a department field on my User model. I am using sql server as my db. I did the following in models.py
class Employee(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, db_column='user')
department = models.CharField(max_length=20, blank=True)
class Meta:
db_table = 'Employee'
Then, using stored procedure I can fill all the fields of User easily, but when I want to fill the extra field department, I get the error
RelatedObjectDoesNotExist at /fetched
User has no employee.
in views.py, where I retrieve the data from procedure:
q = User(id=result_set[i][0], username=result_set[i][1], is_staff=False,
first_name=result_set[i][4], last_name=result_set[i][3], email=result_set[i][8])
q.set_password(result_set[i][2])
q.employee.department = 'something'
q.save()
You can extend the AbstractUser to your model
In models.py
from django.contrib.auth.models import AbstractUser
class Employee(AbstractUser):
department = models.CharField(max_length=20, blank=True)

Saving the Users Profile to the Model

When the form saves I want to save the object with a reference to the user's Profile page. So each profile can list every offer made by the user,
see Class based views query: get objects referenced by another model
ofertoj_oferto.profile_id may not be NULL
My model of "Oferto" contains a ForeignKey to the User's Profile. Right now I don't have an idea how i can tell the system "Get the logged in User's Profile and save this Ofeto with that ID"
Maybe there is some way where I lookup the profile associated with the user and don't need both user and profile on the Oferto model.
But I will still need to a list of every oferto made by the user on there profile
profiles.models
class Profile(BaseInfo):
bio = models.TextField(max_length=15000000)
user = models.ForeignKey(User)
views.py
class OfertoCreateView(LoginRequiredMixin, Turtle_CreateView):
model = Oferto
action = "created"
form_class = OfertoCreateForm
forms.py
class OfertoCreateForm(Turtle_Form):
class Meta:
model = Oferto
fields = ("name",
"description",
"tags",
"time",
"requirements",
"location",
"image",)
models.py
class Oferto(models.Model):
user = models.ForeignKey(User)
profile = models.ForeignKey(Profile)
name = models.CharField(max_length=150)
description = models.TextField(max_length=3000)
time = models.DecimalField(max_digits=10000000, decimal_places=2, null=True)
stelo = models.DecimalField(max_digits=10000000, decimal_places=2, null=True)
location = models.TextField(max_length=3000)
slug = AutoSlugField(('slug'), max_length=128, unique=True, populate_from=('name',))
tags = tagging.fields.TagField()
image = models.ImageField(upload_to='Ofertoj', blank=True, null=True)
requirements = models.TextField(max_length=550000, blank=True, null=True)
def get_absolute_url(self):
return reverse('oferto_detail', kwargs={'slug': self.slug})
def __unicode__(self):
return self.name
def get_tags(self):
return Tag.objects.get_for_object(self)
In your view/form/model save area: calling request.user returns a User object you can send to a model's ForeignKey field