Nodes in network and user model

In my application I’ve got a nodes application with a class Node which is either an Organization or a Person. I created a CustomUser model which extends AbstractUser.

The relation between Node and CustomUser is of type zero or one. I a previous prototype, not built in Django, I added a ForeignKey field to CustomUser that references Node.

However, on second thought, it may be better to add a OneToOneField to Node that references the CustomUser.

What would work best in Django?

Kind regards,

Johanna

In which direction is the “zero or one” relation?

Does one Node have zero or one CustomUser? (You always have a node, that node may have one user related to it.) If this is the case, then the CustomUser would have the OneToOneField in it.

Or does each CustomUser have zero or one Node? (You always have the user. Each user may have one node related to it.) If this is the case, then yes, the Node would have the OneToOneField.

Hi Ken,

Thanks for you reply.

The first option: I always have a node, that node may have one user related to it.

So, the CustomUser would have the OneToOneField in it.

That solved, in this case I need the node to be migrated before I migrate the CustomUser, which lives within the accounts app, don’t I?

Would this work, you think?

(env_code) me@my-mac project_code % python3 manage.py makemigrations nodes
(env_code) me@my-mac project_code % python3 manage.py makemigrations accounts

(env_code) me@my-mac project_code % python3 manage.py migrate

And in settings.py:

INSTALLED_APPS = [
‘django.contrib.admin’,
‘django.contrib.auth’,
‘django.contrib.contenttypes’,
‘django.contrib.sessions’,
‘django.contrib.messages’,
‘django.contrib.staticfiles’,
‘nodes’,
‘accounts’,
]

Or is there a problem with the order in which the apps are set.

Kind regards,

Johanna

There’s no problem with the order in which the apps are identified in INSTALLED_APPS.

Also, you don’t need to makemigrations and migrate separately. If the two changes are made concurrently, makemigrations will keep the order of operations correct.

When it comes to changing your models, in most cases, you can trust Django to do the right thing.

Thank you for your helpful replies. Problem solved!