How do I take user input and then sum/add to the database in django - 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.

Related

How can i make models in django with form input

I Want to make a backend service website, for that I am working in django and want to make django models but with form input,
if a user create a table in my frontend I want to create a table or model in data base how can i do it?
Hello stackies ,
I Want to make a backend service website, for that I am working in django and want to make django models but with form input,
if a user create a table in my frontend I want to create a table or model in data base how can i do it?
No, you cannot create models dynamically because this would require migrations to apply the changes.
I'd recommend that you store your customer information in a structured JSON, that you can store in a PostgreSQL field, for example. Or you can jump to a document-based database, such as MongoDB.

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

How to dynamically swap default database on the model manager in django?

I am creating a project in django and django rest framework. Its an api for an angular app. The database setup consists of multiple databases. one is default database, all the django tables reside in this database; rest of the databases belong to a type of a user, each user is supposed to have a separate database. So, all the user related data goes to its separate database. To implement the selecting database dynamically, user object has an extra field to store the database to write to.
from django.contrib.auth.models import AbstractUser
class CustomUser(AbstractUser):
"""Custom User model."""
database= models.CharField(max_length=9)
Reason for doing this was performance improvement as each database is separate, ListView and DetailView would work faster than if the data was stored in the one database only.
I know I can choose a database to store by using the using method on the model manager. In the rest api things work fine and data is being stored in their separate databases, but I end up overriding methods that django has defined. Its adding development cost to the project. Foreign keys and ManytoMany keys needs to be resolved with the current database of the user, which is not happening as I have customized the database setup. Also, my code cant be as good as theirs :p , as they have written django over the course of many years.
I have overwritten many querysets already, but django still uses default database many times. If only I could use the request object in the model manager of django models to swap the default database on per request basis, things would be different i think.
My questions are -
Is there a way to access the request object in the model manager? I could do something to the effect of below code.
class CustomManager(models.Manager):
def get_queryset(self, request):
return super(CustomManager, self).using(request.user.database).get_queryset()
Model manager has _db property that could be used to select database. Would overriding it is advised? if yes, how and where in the code?
Is there a better way to implement the separate databases?
Thanks in advance.
Regards
Using a database router is recommended in Django docs, but the problem is it only accesses the model class.
Found a couple of questions related to the problem of switching databases dynamically. This post has a solution that would solve the problem of passing the request.user or any other parameter by using a threading.local instance.
Someone created a reusable plugin even for this - https://github.com/ambitioninc/django-dynamic-db-router
Hope that helps.

Django app has multiple database and multiple user

I have written one Django cloud based app. This app will have multiple user and for them multiple database, so that their data should be separate and they can save only to same database.
1) How can we implement it
2) How to automatically one user from login page to assign the database to write on it.
I don't have a complete answer, since you do not give a lot of detail. But here are a couple ots that f hinDjango supports custom database router implementations. A database router is a class that helps django decide which database to use for a particular model. Unfortunately I don't think this mechanism is granular enough for your needs. You can also specify the database to use in your code by using using(name) queryset method and save(using=name) form of save() method for instances. Of course this also means that some features of Django are going to be unvailable to you, since you cannot always expect to have a user. Look at the docs here for more info
https://docs.djangoproject.com/en/dev/topics/db/multi-db/

create an object using the form django

Thank you for looking into this!
I doing one of those django tutorials from their official website, creating a poll. The poll app and everything is working.
The problem is that after I have created authentication for the users to log in they should be able to create the polls, aka they're given the fields with questions/choices, fil it in and from that data(form) it should a new poll object into the db. I have set up everything, but I cannot figure out how do I write the view for this, as in how do I extract data from the form and add it all as a new poll.
I am using three models, as in tutorial: polls, choices and user (user isn't recognisable either, i mean in the model 'user' i have a variable name = models.ForeignKey(User), I was using django-registration to register the them, but that's not the main problem at the moment).
I hope I am more or less clear, if not, I will be glad to explain again:)
thanks, blargie-bla
I'd recommend you starting with generic views.
https://docs.djangoproject.com/en/1.3/topics/class-based-views/#decorating-class-based-views
https://docs.djangoproject.com/en/1.3/ref/class-based-views/#editing-views