model design for considering multiple work schedule with number - python

I am trying to create convincing job hiring model. However I got stuck to show the conditions like 3 Full Time, 2 Part Time in a single job. For example, a company may post job for senior backend developer with job description and all where he may select both Full Time and Part Time work schedule. Probably he want to state how many Full Time and Part Time employer for that job. That is why I am using ManyToManyField for work schedule but I am not sure for the numbers of employeer for certain workschedule in an efficient manner.
Here is the model design that I have designed for now
class Category(models.Model):
name = models.CharField(max_length=200, unique=True)
slug = models.SlugField(max_length=50, unique=True)
description = models.CharField(max_length=400, blank=True, null=True)
class WorkSchedule(models.Model):
name = models.CharField(max_length=100, blank=False, null=False)
slug = models.SlugField(max_length=50, unique=True)
class Position(models.Model):
name = models.CharField(max_length=100, blank=False, null=False)
slug = models.SlugField(max_length=50, unique=True)
class Job(models.Model):
company = models.ForeignKey(
Company, related_name='jobs', on_delete=models.CASCADE)
job_title = models.CharField(max_length=200, blank=True, null=True)
category = models.ForeignKey(Category, related_name='jobs')
description = models.TextField(blank=False, null=False)
city = models.CharField(max_length=100, blank=False, null=False)
position = models.ManyToManyField(
Position, related_name='jobs', blank=False, null=False)
work_schedule = models.ManyToManyField(
WorkSchedule, related_name='jobs', blank=False, null=False)

Related

Python django query database and get all FKs in list

I am new in django and I am working in order to implement a Rest API. My issue is with a query that I can't find a working solution no matter the number of hours spent on it. More specifically I have these two models:
class Subcategory(models.Model):
id = models.UUIDField(primary_key=True, editable=False, null=False, blank=False, default=uuid.uuid4)
name = models.CharField(max_length=50, null=False, blank=False)
category = models.ForeignKey(Category, on_delete=models.CASCADE, null=False, blank=False, db_column='category')
class Category(models.Model):
id = models.UUIDField(primary_key=True, editable=False, null=False, blank=False, default=uuid.uuid4)
name = models.CharField(max_length=50, null=False)
image = models.CharField(max_length=100, default=None)
As you can see each category has one or more subcategories and in the subcategory model I have a foreign key to the category. What i want to do is to query my database, get all categories and to each category add an extra field subcategories with a list of the subcategories. My idea it was to follow the FKs using .select_related but it seems to be a wrong solution since I am taking the following error:
"message": "name 'subcategory__category' is not defined"
My query is:
Category.objects.all().select_related(subcategory__category).values()
Any ideas on how to solve this issue and find a way to implement my query?
Thank you in advance
Do it like this
class Category(models.Model):
id = models.UUIDField(primary_key=True, editable=False, null=False, blank=False, default=uuid.uuid4)
name = models.CharField(max_length=50, null=False)
image = models.CharField(max_length=100, default=None)
sub_cat = ManyToManyField(Subcategory, blank=True)
class Subcategory(models.Model):
id = models.UUIDField(primary_key=True, editable=False, null=False, blank=False, default=uuid.uuid4)
name = models.CharField(max_length=50, null=False, blank=False)
def __str__(self):
return self.name
Now,query part
temp = []
all_cat = Category.objects.all()
for cat in all_cat:
temp.append( list(cat.sub_cat.all()) )
I think this is what you want to do.

signup as multiple user

The use case of my application is I will have different kinds of user. They are:
Agent
Agency
Manufacturer
They have their own kinds of attributes.
1 User
2 Agent
ID
FirstName
MiddleName
LastName
DOB
Sex
Address:
City
Street
Country
Mobile Number
Organization
3 Agency
ID
Name
Address:
City
Street
Country
Contact Number (Multiple numbers can be added)
Email
VAT/PAN Number
4 Manufacturer
ID
Name
Address:
City
Street
Country
Contact Number (Multiple numbers can be added)
Email
VAT/PAN Number
[ Note: Agency have agents. Manufacturer could be
associated with the agency or could have agents directly. ]
For this, I have designed my model in such a way
class Agency(models.Model):
owner = models.ForeignKey(User, on_delete=models.CASCADE)
name = models.CharField(
max_length=200,
blank=False,
null=False)
city = models.CharField(max_length=150, blank=False, null=False)
street = models.CharField(max_length=150, blank=True, null=True)
country = models.CharField(max_length=150, blank=True, null=True)
mobile_number = PhoneNumberField()
email = models.EmailField(blank=False, null=False)
vat_number = models.CharField(max_length=40, blank=False, null=False)
agent/models.py
class Agent(models.Model):
SEX_CHOICE = (
('male', 'Male'),
('female', 'Female'),
)
owner = models.ForeignKey(User, on_delete=models.CASCADE)
agencies = models.ForeignKey(
Agency,
related_name="agents",
on_delete=models.CASCADE)
manufacturers = models.ForeignKey(
Manufacturer,
related_name="agents_manufacturer",
on_delete=models.CASCADE,
blank=True,
null=True)
first_name = models.CharField(
max_length=120,
blank=False,
null=False)
middle_name = models.CharField(
max_length=120,
blank=True,
null=True)
last_name = models.CharField(
max_length=120,
blank=False,
null=False)
date_of_birth = models.DateField(blank=True, null=True)
sex = models.CharField(max_length=6, choices=SEX_CHOICE)
city = models.CharField(max_length=150, blank=False, null=False)
street = models.CharField(max_length=150, blank=True, null=True)
country = models.CharField(max_length=150, blank=True, null=True)
mobile_number = PhoneNumberField()
manufacturer/models.py:
class Manufacturer(models.Model):
owner = models.ForeignKey(
User, on_delete=models.CASCADE, related_name="manufacturer")
agency = models.ForeignKey(
Agency,
blank=True,
null=True,
related_name="agency_manufacturer",
on_delete=models.CASCADE)
name = models.CharField(
max_length=200,
blank=False,
null=False)
city = models.CharField(max_length=150, blank=False, null=False)
street = models.CharField(max_length=150, blank=True, null=True)
country = models.CharField(max_length=150, blank=True, null=True)
mobile_number = PhoneNumberField()
email = models.EmailField(blank=False, null=False)
vat_number = models.CharField(max_length=40, blank=False, null=False)
I have used django-rest-auth for authentication. When i go to /rest-auth/registration the field like username, password1, password2 will be there. But i need which type of user, he/she is either. How do i handle such condition? Because, user should be able to login as either agent or agency or manufacturer. Can anyone help me at this, please?
I would advise against multiple user models. It is because, you introduce complexity to your logic wherever you need to do something with a user.
Moreover, you will get same IDs for different users and you will run into issues to deal with it.
I would recommend to create a custom User model and introduce something called a 'role' which would tell you what kind of user it is.
Then, you can add user profile to your model to add additional information based on the user's role.
Example of user model with a role attribute:
class MyUser(AbstractUser):
ROLE_CHOICES = (
('A', 'agent'),
('G', 'agency'),
('M', 'manufacturer'),
)
role = models.CharField(max_length=1, choices=ROLE_CHOICES)
For the profile, you can add OneToOneField called user profile ( you can check django rest documentation how to do ).
To have different type of registration, you can start with the following view:
class RegisterView(generics.CreateAPIView):
role = None
def perform_create(self, serializer):
serializer.save(role=role)
And use it your urls for different type of roles like this:
urlpatterns = [
url(r'^agents/', RegisterView.as_view(role='agent'), name="agent_register"),
url(r'^agencies/', RegisterView.as_view(role='agency'), name="agency_register"),
url(r'^manufacturers/', RegisterView.as_view(role='manufacturer'), name="manufacturer_register"),
]
you can extend all your models from django.auth.models.User. or create your own user model and extend all your models from it.
when you want to detect type of user use code like this:
if Agent.objects.filter(pk=user.pk).exists():
# its agent
if Agency.objects.filter(pk=user.pk).exists():
# its agency
if Manufacturer.objects.filter(pk=user.pk).exists():
# its manufacturer
to extend your models from user model just update your code same as below:
from django.auth.models import User
class Agent(User):
<same as your example>
class Agency(User):
<same as your example>
class Manufacturer(User):
<same as your example>

How to undisplay the field with empty value in django admin?

I am currently working on django. I created 4 classes in models.py, one of them is ReactionMeta class. This class has 62 columns, which defined as below:
class Reactionsmeta(models.Model):
id = models.ForeignKey('Reactions', db_column='id', primary_key=True, max_length=255, on_delete=models.CASCADE)
name = models.CharField(max_length=255, blank=True, null=True)
metabolite1 = models.ForeignKey('Metabolites', db_column='metabolite1', blank=True, null=True,
on_delete=models.CASCADE)
stoichiometry1 = models.CharField(max_length=255, blank=True, null=True)
metabolite2 = models.ForeignKey('Metabolites', db_column='metabolite2', blank=True, null=True,
on_delete=models.CASCADE, related_name='+')
stoichiometry2 = models.CharField(max_length=255, blank=True, null=True)
metabolite3 = models.ForeignKey('Metabolites', db_column='metabolite3', blank=True, null=True,
on_delete=models.CASCADE, related_name='+')
stoichiometry3 = models.CharField(max_length=255, blank=True, null=True)
metabolite4 = models.ForeignKey('Metabolites', db_column='metabolite4', blank=True, null=True,
on_delete=models.CASCADE, related_name='+')
#...
Some of the reactions, may have 6 metabolites, however, some of reactions may only have 2 metabolites and stoichiometries, which means there is no value of this reaction in metabolite3,4,5,6...columns and stoichiometry 3,4,5,6...columns.
In that case, how can I only display the Charfield with data while undisplay those Charfield with no value in django-admin?
So I think there is model design issue here. For my case i would have done this as follows
class ReactionMeta(models.Model):
reaction = models.ForeignKey(Reaction, ..)
name = models.CharField(..)
data = models.ForeignKey(Data, ..)
Contructing the Data class to hold the Stoichiometry and Metabolite
class Data(models.Model):
stoichiometry = models.CharField(..)
metabolite = models.ForeignKey(..)

Getting started building a template/view for a django model that joins two models together

I have a form where I can create and edit a group (a business really). Though I want to be able to say this business has many locations. I have the models written but the UI is giving me trouble.
I think mostly my question would be answered by how to update the Group model (creating/editing a group) and the GroupLocations model with a single form (adding an address as a new location if need be) so almost three models with a single form?
The models are:
class Address(models.Model):
city = models.CharField(max_length=50)
state = models.CharField(max_length=50)
zip_code = models.CharField(max_length=50)
address_line_one = models.CharField(max_length=50)
address_line_two = models.CharField(max_length=50, null=True, blank=True)
contact = models.CharField(max_length=50, null=True, blank=True)
phone = models.CharField(max_length=50)
fax = models.CharField(max_length=50, null=True, blank=True)
created_at=models.DateField(auto_now_add=True)
updated_at=models.DateField(auto_now=True)
def __str__(self):
return self.city
class Group(models.Model):
group_name = models.CharField(max_length=50)
group_contact= models.CharField(max_length=50)
tin = models.CharField(max_length=50)
npi =models.CharField(max_length=50)
notes = models.TextField(max_length = 255, null=True, blank=True)
billing_address = models.ForeignKey('Address', related_name = 'billing_address', on_delete=models.SET_NULL, null=True)
mailing_address = models.ForeignKey('Address', related_name = 'mailing_address', on_delete=models.SET_NULL, null=True, blank=True)
start_date = models.DateField(auto_now=False, auto_now_add=False, null=True, blank=True)
end_date = models.DateField(auto_now=False, auto_now_add=False, null=True, blank=True)
change_date = models.DateField(auto_now=False, auto_now_add=False, null=True, blank=True)
change_text = models.TextField(max_length = 255, null=True, blank=True)
term_comment = models.TextField(max_length = 255, null=True, blank=True)
group_phone=models.CharField(max_length=50)
group_fax = models.CharField(max_length=50)
group_term = models.ForeignKey(GroupTerm, on_delete=models.SET_NULL, null=True, blank=True) #quesiton is can a group be termed many times?
created_at=models.DateField(auto_now_add=True)
updated_at=models.DateField(auto_now=True)
#provider_location = models.ManyToManyField('ProviderLocations', through='GroupLocations')
def __str__(self):
return self.group_name
class GroupLocations(models.Model):
address = models.ForeignKey('Address', on_delete= models.SET_NULL, null=True)
group = models.ForeignKey('Group', on_delete=models.CASCADE)
doing_business_as = models.CharField(max_length = 255)
created_at=models.DateField(auto_now_add=True)
updated_at=models.DateField(auto_now=True)
def __str__(self):
return self.doing_business_as
I would love to (for this model and others) mimic how the admin handles users with the nice left and right multiple select boxes: (Though not sure how the add new location maybe a button to take you to another form that somehow knows its supposed to be added to the group as well?)
I realize this is a very large question I am looking for just mostly I think how to deal with several models on one form/template rails had a word for this (the name escapes me now) and not sure what the Django paradigm is.

Do not allow same value in different columns for the same record in django

I am building a website in django that will allow players to be matched with other players. I have the following model:
class Session(models.Model):
# id = AutoField(primary_key=True) added automatically.
sport = models.ForeignKey('Sport', unique=False, blank=False, null=False, on_delete=models.CASCADE, )
hostplayer = models.ForeignKey('Member', unique=False, blank=False, null=False, on_delete=models.CASCADE, related_name='member_host', )
guestplayer = models.ForeignKey('Member', unique=False, blank=True, null=True, on_delete=models.CASCADE, related_name='member_guest', )
date = models.DateField(blank=False, null=False, )
time = models.TimeField(blank=False, null=False, )
city = models.ForeignKey('City', unique=False, blank=False, null=False, on_delete=models.CASCADE, )
location = models.CharField(max_length=64, unique=False, blank=False, null=False, )
price = models.FloatField(unique=False, blank=False, null=False, default=0, )
details = models.TextField(unique=False, blank=True, null=False, )
def __unicode__(self):
return unicode(self.id)
As you can see, both hostplayer and guestplayer are foreign keys of the Member table.
The problem is that when I go into django admin, if I select the hostplayer as jack, I can also select the guestplayer as jack. This is obviously wrong as a player cannot play against himself.
How can I limit the options of guestplayer to not include the hostplayer?
Also, on the models level, is there a way to specify that the value of one attribute must be different from the value of another attribute in the same tuple? An obvious way would be to use forms and validate them but I am curious whether a simpler alternative exists.

Categories

Resources