Should i use proxy models in django? - django

im here trying to start a new project, in django
and i came across this problem, while creating the models
My users will be different types of users, so i was thinking, should i use proxy models for it?
because here is how it´s going to be the realtionship between them.
i will have a client, and a personal trainer, and the admin of course, but the problem here is that the client is going to have a ForeignKey with personal trainer, my main question is, can i create in a proxy model an extra field where i establish the relationship with personal trainer proxy model?
If not, is there any other way that i can aproach this type of situation
Thanks in advance

No. As far as I know a Proxy Model only changes the behavior of a model. You can add new methods only. If you want to add new fields it must be a Concrete Model.
You should create a new model class which inherits from AbstractUser. Then you can either add a type field which can be "client" or "personal_trainer" or "whatever". Or you could use a OneToOne field to have a UserProfile associated w/ your user - a different profile for "clients" or "personal_trainers" or "whatevers".

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

Django User inheritance

I am building a food ordering website with Django. I want my users to register an account on my site, and they should sign in to actually order. I want to use the User class built in with Django, but that doesn't include necessary fields like address, confirmation ID, and phone number. If I build a custom User model, that doesn't have many good helper functions which I can use like auth.authenticate. I searched this topic, and I found that I could use AbstractUser. But when I inherited my CustomUser class from AbstractUser, some strange things began to happen. After some more research, I found out that changing the User model after applying my built-in migrations give some errors as there are some relationships or something.
I deleted my database and created a new one. Now, I am extending my CustomUser class from the built-in User class. This works fine, only you can't do auth.authenticate checking with the, confirmation ID for instance. Also, it seems to create two models every time I create a new CustomUser, the other on in the Users under the auth tab.
Can you tell me any good way to connect the User model with a few more fields after applying the built-in migrations? Thanks in advance.
You should extend from AbstractUser and not User class ( behaviour you are experiencing is Multi-table inheritance (as documented))
Whole process of substituting default user model is well documented

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.

Associating auth_user with a model user in django

I'm creating an application in django that will eventually allow users to post pictures. The Pictures contain a ForeignKey to the User.
I want to be able to manipulate the User class as a model class as above, but I also want it to serve the functions of authorization etc., which django-registration/auth is handling right now.
So basically I have two classes; my own User class, and the auth_user class, each with their own tables in sqlite.
How should I go about associating the two? Should I use a OneToOne field or should I just extend the auth_user class to include all the functionality of the model User class?
I believe this is what you're looking for:
https://docs.djangoproject.com/en/1.4/topics/auth/#auth-profiles
You'll need to specify your custom class within your settings.py file using something like:
AUTH_PROFILE_MODULE = 'accounts.UserProfile'
Hope that helps.

Django add a many to one field to user object

I'm creating a Network class that has a many-to-one relationship with the Django User class. In other words, a Network can have many User and each User has only one Network. It's easy to add many-to-many relationships because I can add the many-to-many field in the Network class; however, it's hard to apply many-to-one relationship since I need to add the the foreign key in User class. There is no way I can do that.
I do have a UserProfile class that has a one-to-one relationship with User, but it's only for storing additional information about the User, not any relationships. All my other relationships defined are relating to User, not UserProfile.
Is there a way to create a many-to-one relationship to the User and Network class without using the UserProfile? Thanks!
The only things I can think of are subclassing User, which is tricky because the admin has no way to turn a base object (User in this case) into a subclassed object (NetworkUser, and yes I've tried), or creating a NetworkUser model with a OneToOne relationship to User and a ForeignKey to Network. I'd recommend the latter, because while you feel sure now that you just want to add one thing to User and it shouldn't be a big deal, later on there will be more to add.
The thing that bothers me about directly messing with User is that this is only for a single app, not necessarily the whole project, and modifying User in a way that will affect other apps in the project.