Custom user model

Hi everyone, I am new using Django and I am trying to build a project that requires to use a Custom User Model. I have encountered many problems on my way, tried to resolve them on my own but after more than one week I am exhausted, so I ask for your help.

I think the main problem is that I don’t understand well how to create a CustomUser model. I need a user to have many fields (E.g full_name, mobile_phone, etc), but one of the critical fields which I’ve had problems is: division_id (wich is a foreign key for a model created in another app named ‘divisions’). I’ve had a lot of problems because there is no any migration yet (its supposed that your first migration must be when you created your custom user model), so there isn’t any values in ‘division’ model. Is it possible to refer a foreign key in that case considering that the model referred isn’t created yet? I mean, the first migration can create a custom user model and a division model simultaneusly, or what I have to do?

Another problem I have, is that when I’m trying to refer the Division model into my CustomUser model, I have errors. I use relative import but it returns ‘attempted relative import beyond top-level package’
(I know what it means, but for me, my relative path is OK, I don’t understand why it doesn’t work).
This is my relative path: from …divisions.models import Division

This is my project structure:

I am trying to reference the model Division (which inside divisions.models) from my CustomUser model (which is inside users.models). I read that a ‘.’ means ‘from the same folder, import…’ so I used ‘…’ because the Division model is in another folder, but didn’t work. Hope you can help me please

Creating a Model containing a ForeignKey to a table without data is ok. You’re not actually making the association to a row yet, you’re just telling the database that you will be making a connection.

However, trying to create a ForeignKey field if the Model doesn’t exist (or isn’t being created in the same migration) will cause a problem.

Regarding relative imports - when you’re running Django, your imports are all done relative to your base directory, not the current directory in which the code you’re currently writing resides.
So, from any file in any location within your project, you can import your Division model as from divisions.models import Division.

1 Like

Thank you for your answer Ken. I’ve already tried to import my Division model using what you suggest:
from divisions.models import Division

But in that case, I get the following error:
ModuleNotFoundError: No module named 'divisions’

For that reason, I tried using relative paths, but neither option worked

Different problem then.

Relative paths are not the answer.

What’s your settings.py look like?

  • What’s your BASE_DIR?
  • Did you add divisions into your installed apps?

Sorry, my bad. It seems I forgot to add into installed apps. Rookie mistake I guess…

Thanks for your help Ken!