Django table model - django

I am relatively new to Django and would like to use it to build the back end of my app. I am using Django.DB models and Django rest-framework. I am trying to make a system where each company in my DB has a table that stores 3 records. Item, Price, and Description but I have no idea what type of field that would be or how one would do that. I was assuming that you would use a one-to-many field but there doesn't seem to something like that.
Thank You in advance.

Related

How can I implement authentication in Django

I am new to Django.
I am going to build simple register and login fullstack application by using React and Django.
My problem is when I received register request with form data.
Is it ok to create custom table for users?
I am going to create another table related to user table.
So in that case, there must be id in the users.
That's why I am going to create custom table.
Please help me it is good practice.
You can abstract from AbstractBaseUser and then you can customise the user model and to specify it in your settings file.
Please see the django documentation here:
https://docs.djangoproject.com/en/4.1/topics/auth/customizing/#substituting-a-custom-user-model
In Django you can link between multiple table by different relationships depends on what you want like:
OneToOne
ForeignKey
ManyToMany
And by default when you create a model django create a pk field it is ID for table, you can make another field as a primary key for model
When you use one of those relationships django by default use model id to link between them
and you can also create a custom user model to use it
Good luck

Do all tables need to be built as models?

I am new to Django and trying to figure out models. I have a question that I just cannot find the answer to. I am designing an equipment checkout app. I would like to have a data entry page (/Admin) where we can enter the equipment names and types...no problem (I think). However, I also need a Checkout table that will be updated as equipment is checked in and out and an Employee table that would hopefully pull from AD. My question is, do these two tables that will not be updated manually on the Admin page still need to be defined as models? Is this where the 'EDITABLE' field option comes in? Thanks!
Models in Django are the main interface to communicate with data layer. They map directly to database, provide access to it using ORM and also track changes via migrations. They can be integrated with pretty much all Django components: generic views, forms, serializers, etc.
Just because certain piece of data is not being updated manually, doesn't mean that you don't need to define models for it.
If you want to use database tables separately from models, you'd need to manually create DB connection and use raw SQL or create custom data model. Unless you're using a third party external service, I don't think there are many use cases where that would be required.
well i would do it like that
models.py
class Equipment (models.Model):
name = models.CharField(max_length=30)
types = models.CharField(max_length=30) #choice field here would be better
serial = models.CharField(max_length=30)
employee = models.ManyToManyField("Employee")
class = Employee (models.Model):
#here you add all employee data
this way you could create an instance from the admin for every equipment and employee and on every checkout you can update the equipment

Database table for bus booking website

I'm creating a bus booking website using Django. I'm stuck at what the database tables should look like. So far I've created the displayed tables, I'm very poor with databases so please mention foreign keys and primary keys when answering. Also, I'm stuck on how to actually book seats in Django, like what will be the code in the background and what other packages should I use. Thanks.
Table- Route:
Columns - route_id (primary key),
location_from,
location_to,
route_name,
Table - Bus:
Columns - Bus_id(primary key), type_of_bus,
bus_registration,
capacity,
bus_number,
route (foreign key)
Also I'm using the default sqlite database that comes with django. Is it good enough to build this kind of website or do I need to change? This website is just a project and will never go into production phase.
I'd highly recommend writing your database schema in Python classes (as Django models) and then using ./manage.py makemigrations ./manage.py migrate, which will look at your model code and create the corresponding database schema. This portion of the django tutorial could be helpful to you.
The python code corresponding to your example for a route would look something like:
from django.db import models
class Route(models.model):
location_from=models.CharField()
location_to=models.CharField()
route_name=models.CharField()
This is a much easier way to write a database schema, especially if you're not so hot with databases.
If your app will never be in production, SQLite is fine - it's very similar to other database engines for toy projects.
As for what your data model should look like, I think it depends how complex you want to make the app. For a start, I think you could add a Location model, which your Route model should reference. For making reservations, you could look into django booking - I haven't used it but it comes up on django packages when you search 'booking'.

How do I take user input and then sum/add to the database in django

I am trying to get the user input and add to the database.
Example:
A user is going to type an integer, then this is to be added to the current value in the database. It's like sending money to someone via online banking.
Assuming you want to use Django's ORM, you will need to define a model. In your model you will have fields that define the data items to store. After you've had Django's manage.py tool setup the database, you can create instances of your model saving them with the save method of your model instance.
This probably seems like a lot of hand waving, and it is. What you should do is work through the Django tutorial as it will answer your questions. The tutorial is at the Django documentation site.

When you change the fields in the models in django

After you initially create the model and you want to change the fields in the models. If you syncdb ,it replies no fixtures found, meaning to say the models has not recognised the change. One way to solve this is to delete the database and recreate the database but you lose all the data. Is there any other better solution? Thanks...
Use south to help you for models changes. It will automatically detect any changes to models and generate appropriate script to update db schema.