Database design and Django Model for Contact and Enquiry - django

I'm working on a small Django website for a photographer.
There are a contact form and a booking. A percentage of these contact leads will be lost. If the customer books, the app should be able to follow up with the customer.
I've been thinking about how to proceed and my idea is to create a contact table with the information that the customer provides from the contact form and a second table that would have a "One to One" relationship in case that the lead becomes a customer.
In a later stage after the event has happened, there will be a private access folder where the user can download his pictures. And has to be a user in order to be able to log in and have authorization and authentication.
How would you design the database and the models in Django? Any advice or considerations in order to tackle de problem in the best way possible?
Thanks for your help!

models.py
class contact(models.Model):
user = models.OneToOneField(User)
Phone = models.CharField(default='', max_length=20)
event_type=models.CharField(default='',choices=create_a_list_of_choices_)
date=models.DateField(auto_now=True)
email=models.EmailField()
image = models.ImageField(blank=True,upload_to='users_photos',)
class Booking(models.Model):
User=models.ForeignKey(
User,
on_delete=models.CASCADE,
)
Extra=models.CharField(max_length=50,blank=True)
Deposit=models.CharField(max_length=50)
Time=models.DateField(auto_now=True)
Notes=models.TextField(max_length=250,blank=True)
Price=MoneyField(max_digits=14, decimal_places=2, default_currency='USD')
this is how your models should look like you can add some more other fields, and for the moneyFiled you can install it by following this link :https://github.com/django-money/django-money

Related

How to best create connections for a booking app?

I am new to Django and relational databases coming from the Firebase world. I am having trouble figuring out the best modeling for a Doctor-Patient booking app and generally how relational DBS works best; I would like to minimize future problems by doing a great job now. I am going to use Django and Django Rest Framework at the backend to feed a React frontend.
So far, I've created these models in a clinic app. Patients and Secretaries are going to be part of the users, and so are Doctors. I then create the Serializers and Viewsets for the API.
class Clinic(models.Model):
name = models.CharField(max_length=200)
description = models.TextField()
accepted = models.BooleanField(default=False)
class Doctor(models.Model):
clinic = models.ManyToManyField(
Clinic, related_name="doctor")
first_name = models.CharField(max_length=200)
last_name = models.CharField(max_length=200)
email = models.EmailField(max_length=240, default="email")
appointment_duration = models.IntegerField(default=20)
class Secretary(models.Model):
clinic = models.ForeignKey(
Clinic, on_delete=models.CASCADE, related_name="secretary")
name = models.CharField(max_length=200)
email = models.EmailField(max_length=240, default="email")
doctors_responsible_for = models.ManyToManyField(Doctor)
class Patient(models.Model):
first_name = models.CharField(max_length=200)
last_name = models.CharField(max_length=200)
email = models.EmailField(max_length=240, default="email")
date_of_birth = models.DateField()
age = models.PositiveIntegerField(default=0)
Should I create a User model to be able to differentiate users (Doctors, Secretaries and Patients)? They are all going to be able to register and log in and each will see different things on the page. Should I just return 'is_doctor' or 'is_secretary' from the serializer API and show different content from there?
I'm confused as to how I would connect a User model with a Doctor or Secretary model, for example, or if I even need to since they're all users...
How would I differentiate users (Doctor, Secretary, Patient) at the registration moment? E.g., for each of them to have a different registration form with a boolean for is_doctor, is_secretary?
I can't to come up with a solution for storing booked appointments. I'm wondering if I should create a new model, Bookings, for saving bookings but I'm not sure if this booking model should hold every single booking (from any patient to any doctor), considering this app will be used by a lot of people. Or should bookings be under each patient and each doctor?
In this case, secretaries will also be able to manually add bookings to a Doctor calendar and add the patient as well.
I am building all of this in a single app, clinic, perhaps it is recommended to create different apps for this?
Each doctor will need to have its own calendar for this app to work, with say, 'day 12, blocks of 20mins from 09:00 to 11:30'. Should I create a Calendar model? Or how is it best to achieve this? How to best come up with this model? This calendar will be populated with blocks of time from whatever each doctor chooses as their availability.
First of all, I'm a django noob, so please read the following with that it mind.
Looks pretty good - the only thing I see missing is how you link patients to clinics and or doctors.
The other thing I notice is how doctors can have multiple clinics. I assume each clinic has its own calender, rather the doctor itself? Or maybe both? i.e. Even if a doctor was available on his calendar, he might not have a room at the clinic for the patient as other doctors' calenders would clash with it.
Personally, I wouldn't create a new app for clinic unless you want to model it in far more detail. Keep it simple initially.
Also, if you're allowing doctors, secretaries, and patients to login to your site, it might be better to have consumer/provider class model descending from custom user. ideas...
I would start thinking about the problem in more abstract terms. Service/provider/consumer.
But, I think you're on the right track.
I can throw in some ideas.
Models
I think you are on the right track. You just need to associate models Doctor, Secretary and Patient to the User model. I would recommend you to create a custom user model inherited from AbstractUser.
In this model, you can either add a choice field with choices for each type of user. link to docs
Also, you need to link the user model with the correct model.
One way to do is to have a OneToOneField for the user model in all your user type models: Doctor, Secretary, Patient
Or you can explore generic relations. It will further streamline things for you. link to docs.
Signup
You can provide a field for users to select at the time of signup, or provide separate links to signup and handle things at the backend. Something like If you are a doctor, click here to signup. In both cases, you'll need to override the signup process.
So a signup link can look like: /signup/doctor/ or /signup/patient/. All signup will be using the same view, just different url kwargs. link to docs
You can just create rows on the relevant model for user type on form success.
Booking
Yes, you need to create a separate model, and you can store all your bookings in this model. Doesn't matter how many users use your app. Just use a good database solution, like Postgres. There are ways to optimize your queries, like indexing, don't worry about it for now. Just make sure to save all references like, patient, doctor, created, last modified, created by which user, from_datetime, to_datetime, etc.
It would be better to handle the 20 min appointment blocks in forms.py.
You can create a list of acceptable time blocks, so that if in future you want to change this time to say 30 min, its easily doable. Just handle all validations at the form level and it should do the trick.

What I should write in my models.py to connect two ManyToMany forms?

I want to add auto-service, it has list of brands of cars and services in M2M field.
For each brand of car I should link several services.
Please help me to write code I'm a beginner trying to make website.
here is url to picture, take a look
https://ibb.co/31DZZJ1
Well,I don't want to write everything. But Basically, you will have two models.
class CarBrand(models.Model):
name = models.CharField(max_length=200)
services = models.ManyToManyField('Services', blank=True)
class Services(models.Model):
service_name = models.CharField(max_length=200)
Then you can add these models to your admin and once you fill the services model admin will show you a list of services and you can select the ones you want for each car brand in the car brand model..
Sorry I am not good at explaining. but ask me if you don't understand

How to implement two authentication layers in django

I have a web application for businesses. Upon login, a user (the manager of the business) can view tables with information about their business.
userID 1->* rows in a MasterTable,
userID 1->* rows in a ForecastTable
I need to change it so an owner of multiple businesses can log into the account for a specific business and edit the same information that the manager can.
I was thinking about changing the database schema to something like this:
userID - businessID 1-* rows in MasterTable, rows in ForecastTable
Should I have 2 login pages, first for the userID, then the businessID. Then all the columns in the database only reference the businessID, so different users can edit the same data.
Or the same login form makes the user enter a businessID, and their username, then depending on which businessID they enter, it logs into that page?
I'm not sure what is the best practice to implement something like this.
Here is what my django model looks like:
from django.db import models
from django.contrib.auth.models import User
class MasterEntry(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
date = models.DateField()
cutoff1 = models.IntegerField(default=0)
cutoff2 = models.IntegerField(default=0)
rooms_sold = models.IntegerField(default=0)
is_blackout = models.BooleanField(default=False)
class ForecastEntry(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
date = models.DateField()
rate = models.IntegerField(default=0)
A user has hundreds of these 'master entry' and 'forecast entry' rows. I query the database for the rows and create a table in the front end.
You don't need two layers of authentication.
For instance, a user which is not admin only can view business he owns, this is achieved with a simple filter in the view displayed the mentioned list of business.
And you could render each business name as a link that then shows a list of MasterEntrys in it.
This is more a problem with information layout.
Conclusion:
Show a page only with business belonging to the authenticated user.
Superuser can see all business.
You can click a business entry in order to view/edit/delete any of the MasterEntrys it contains.

Django Database structure, How to handle slug from two different models like Facebook does?

I have a project that publishes job offers, a job seeker can create a profile on the website and his profile page will have URL like:
www.domain_name.com/slug
An institution publishing job offers can also create an account.The person actually creates the institution account will be considered as the administrator of the institution page, so he will have an account (*instance of UserProfile() and can add other members (Users on the platform not affected to any institutions) to that institution. Institution profile URL will be www.domain_name.com/institution_slug
Members will have their own UserProfile instance linked to that institution, and once connected, only the institution page data/functionalities will be visible, they will work as the institution.
So What is the best way to have these 2 models and handle the URL pattern properly? like
urls.py
path("<slug:slug>",profile,name='profile')
views.py
def profile(request,slug):
user = UserProfile.objects.get(slug=slug) #get_object_or_404
inst = Institution.objects.get(slug=slug) #get_object_or_404
In above example, I don’t want to have to query twice in two different models. Is there another way to do it? Or If I need to re-design the database structure, let me know!
models.py
UserProfile(models):
# info_about_user
sug = models.SlugField()
institution = models.ForeignKey(Institution)
Institution(models.Model):
# info_about_instituion
slug = models.SlugField()
Note that both may publish job offers:
Job(models.Model)
owner = UserProfile or Institution

How to save two different users in a django model?

My case is related to a purchase, the customer who buys something and the seller who sold it.
Models.py
from django.contrib.auth.models import User
class buy(models.Model):
customer = models.ForeignKey(User)
seller = models.ForeignKey(User)
I am aware that the above code is wrong, I write it that way so the question is understood.
I take the django.contrib authentication system, to avoid having to make another authentication system for clients and one for sellers, I want django code reuse.
A solution had thought of creating another data model to sellers or customers, but in my view and in the login I'm using django.contrib, then I would still use this system authentication would like to know if there is any way or if I ultimately that create another authentication system?
I'm just guessing, if you have a Product model that has a user field in which case he's the actual seller, why don't you use seller = models.ForeignKey(Product, to_field='user')