Custom id AutoField in Django model - python

i want create a custom field with AutoField
description:
Code generated by the system is increasing gradually, the code consists of 5 digits, there are no 2 identical codes. For example: 00001, 00002
this is my code :
class Supplier(models.Model):
code = models.AutoField(primary_key=True, max_length=5, blank=False, null=False)
name = models.CharField(max_length=200, blank=False, null=False)
phone = models.CharField(max_length=11, unique=True, validators=[len_phone])
email = models.EmailField(blank=True, null=True, unique=True)
address = models.CharField(max_length=200, blank=False, null=False)

Related

IntegrityError Django Model

I'm trying to implement a foreign key at one of my tables, but I keep getting this error all the time:
null value in column "user_id" of relation "application_application" violates not-null constraintDETAIL: Failing row contains (3, 2022-09-09, San Ramon, CA, USA, https://logo.clearbit.com/uber.com, Uber, , Applied, Engineer, null).
I don't know what I'm doing wrong
from django.contrib.auth.models import User
class Application(models.Model):
__tablename__ = "applications"
name = models.CharField(max_length=50, blank=False, null=False)
title = models.CharField(max_length=50, blank=False, null=False)
status = models.CharField(max_length=50, blank=False, null=False)
notes = models.TextField(blank=True, default="Add some notes", null=True)
location = models.CharField(max_length=255, blank=False, null=False)
logo = models.CharField(max_length=255, blank=False, null=False)
date_applied = models.CharField(max_length=255, blank=False, null=False)
user = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.name
It looks like you're not passing in the user when you're trying to save. Try this:
app = Application(user=request.user, name='App X', etc)
app.save()

Django: how to upload media files to different folders?

I want to save file in automatically created folder related with Employee id_number like:
media->employee->attachments->emp001->emp001.pdf
models.py
from django.db import models
class Employee(models.Model):
id_number = models.CharField(primary_key=True, null=False, blank=False, unique=True, max_length=15)
full_name = models.CharField(max_length=255, null=False, blank=False)
name_with_initials = models.CharField(max_length=255, null=False, blank=False)
surname = models.CharField(max_length=255, null=False, blank=False)
phone = models.CharField(max_length=15, null=False, blank=False)
dob = models.DateField(null=False, blank=False)
gender = models.CharField(max_length=10, null=False, blank=False)
email = models.EmailField()
address = models.CharField(max_length=255, null=False, blank=False)
class EmployeeAttachments(models.Model):
employee = models.ForeignKey(Employee, on_delete=models.CASCADE)
cv = models.FileField(upload_to=f'employee/attachments/', max_length=100)
can any one tell me how to do this in django, django-rest-framework
This will change a path of cv on EmployeeAttachments according to your Employee id
def facility_path(instance, filename):
return f'attachments/employee/emp{instance.employee.id}/emp{instance.employee.id}.pdf'
class EmployeeAttachments(models.Model):
employee = models.ForeignKey(Employee, on_delete=models.CASCADE)
cv = models.FileField(upload_to=facility_path, max_length=500)

problem with multiple user dependent on each other in Django

I am working with some custom-made user models in Django. They are the following:
myCustomeUser responsible for the primary identity of a user
Industry is a user that will link with OneToOneField to the myCustomeUser
Employee is another user account, which will FK to the myCustomeUser and FK to Industry
my models.py:
class myCustomeUser(AbstractUser):
id = models.AutoField(primary_key=True)
username = models.CharField(max_length=20, unique="True", blank=False)
password = models.CharField(max_length=20, blank=False)
is_Employee = models.BooleanField(default=False)
is_Industry = models.BooleanField(default=False)
class Industry(models.Model):
user = models.OneToOneField(myCustomeUser, on_delete=models.CASCADE, primary_key=True, related_name='industry_releted_user')
name = models.CharField(max_length=200, blank=True)
owner = models.CharField(max_length=200, blank=True)
license = models.IntegerField(null=True, unique=True)
industry_extrafield = models.TextField(blank=True)
Now I need to write the model of Employee. There are some conditions also:
It should contain name, National ID, gmail, rank, employee_varified, named fields
This will inherit the myCustomeUser and Industry both
The Industry account user will primarily entry all the data of Employee in the database, except username and password(which are inherited from myCustomeUser)
Later on, the Employee will search his National ID given by the Industry and finish the registration process by creating his username and password.
I have tried the Employee model like this:
class Employee(models.Model):
user = models.ForeignKey(myCustomeUser,primary_key=True, null=True, on_delete=models.CASCADE)
industry = models.ForeignKey(Industry, on_delete=models.CASCADE)
National_ID = models.IntegerField(null=True, blank=False, unique=True)
name = models.CharField(max_length=200, blank=False, null=True)
gmail = models.EmailField(null=True, blank=False, unique=True)
rank = models.CharField(max_length=20, blank=False, null=True)
employee_varified = models.BooleanField(default=False, blank=True, null=True)
But the problem with this model is I cannot create any Employee object without giving user (that means username and password), But the Industry user needs to entry their Employee's data, before complete the Employee's registration.
how can I write my Employee model to solve this problem?
If you can't guarantee that a related object will exist when you create an object, you can make the relationship(s) optional.
So in your case, I'd create your model more like;
class Employee(models.Model):
user = models.ForeignKey(
myCustomeUser,
blank=True,
null=True,
on_delete=models.CASCADE
)
industry = models.ForeignKey(
Industry,
blank=True,
on_delete=models.CASCADE
)
national_id = models.IntegerField(
null=True,
blank=False,
unique=True
)
name = models.CharField(
max_length=200,
blank=False,
null=True
)
# ... etc
You may also benefit from having a look through the following site which might help you learn a thing or two about django
https://www.django-antipatterns.com/

referral by company's product specific and company specific

I am thinking of creating a referral and reward app where a user will list his/her company with the product they have. A company can use referral program by product specific or just in whole(could not name it properly). For example, I have listed my company called ABC Company and I have a product like smartphone, smart Tvs, Laptops. I would like to market for my company by just saying refer me to 10 people and get something in return(this is non-product specific) or I should be able to market my specific product let's say when user goes to the abc phone XI and there will be refer this phone and get the same phone in return if you refer to more than 50 or if more than 10 then 10% discount etc. This is just an example to demonstrate my project.
For now I created the model for Company, Product(with nested category), referral. But I have no idea on how should i be able to keep the referral based on above example like product specific or based on full company.
Here is what I have done
class Product(models.Model):
"""
product model
"""
name = models.CharField(max_length=100, blank=True)
company = models.ForeignKey(Company, blank=False, null=False, on_delete=models.CASCADE)
category = TreeForeignKey('Category', null=True, blank=True, db_index=True, on_delete=models.CASCADE)
image = models.FileField(upload_to='/company/', max_length=100, blank=True, null=True)
description = models.TextField(blank=True, null=True)
stocks = models.IntegerField(default=0, blank=True)
class Company(models.Model):
"""
company model
"""
name = models.CharField(max_length=150, blank=False, null=False)
domain = models.URLField(blank=False, null=False)
email = models.EmailField()
description = models.TextField(blank=True)
pan_number = models.CharField(max_length=100, blank=False, null=False)
industry = models.CharField(max_length=150, blank=False, null=False)
class Join(models.Model):
"""
Join Model
"""
email = models.EmailField()
friend = models.ForeignKey("self", related_name='referral', null=True, blank=True, on_delete=models.CASCADE)
ref_id = models.CharField(max_length=120, default='ABC', unique=True)
count_added = models.ForeignKey("self", null=True, related_name='count', blank=True, on_delete=models.CASCADE)
ip_address = models.CharField(max_length=120, default='ABC')
timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
updated = models.DateTimeField(auto_now_add=False, auto_now=True)
def __str__(self):
return '{}'.format(self.email)

create custom role in django to employees

I have created a role model for Employee so that employee will be assigned to control
the overall app based on his/her role. I mean if the role of employee is given can_create_only, then the employee should be able to create inventory, orders, items etc and if employee is given can_create_edit_and_delete, then the employee would be like one of the admin and etc. For this I have designed the model as below but I want to know what is the best way to handle such and why?
Should I go with middleware or decorator way? Can anyone give me an example, please?
class Role(models.Model):
name = models.CharField(max_length=100, blank=False, null=False)
class Meta:
verbose_name = 'Role'
verbose_name_plural = 'Roles'
class Employee(models.Model):
office = models.ForeignKey(
OfficeSetup, blank=False, null=False, on_delete=models.CASCADE)
name = models.CharField(max_length=150, blank=False, null=False)
designation = models.ForeignKey(Designation, blank=False, null=False)
section = models.ForeignKey(DepartmentSetup, blank=True, null=True)
phone_number = models.CharField(max_length=150, blank=True, null=True)
mobile_number = models.CharField(max_length=150, blank=True, null=True)
email = models.EmailField(max_length=150, blank=False, null=False)
gender = models.CharField(
max_length=4, choices=GENDER, blank=True, null=True)
role = models.ForeignKey(Role, blank=True, null=True)
username = models.CharField(max_length=100, blank=False, null=False)
password = models.CharField(max_length=100, blank=False, null=False)
avatar = models.ImageField(
null=True, blank=True, upload_to=upload_employee_image_path)
class Meta:
verbose_name = 'Employee'
verbose_name_plural = 'Employees'
def __str__(self):
return self.name
When creating an employee by admin, the username, password and email, the admin provides will create a new user instance along with the employee
Django comes with Groups and permissions which provides all most everything you are looking for.
This may help you - How do I use Django groups and permissions?
Django documentation - https://docs.djangoproject.com/en/1.11/topics/auth/

Categories