I created Users inside the admin view with my custom User model:
class User(AbstractUser):
phone_number = models.CharField(max_length=64)
department = models.CharField(max_length=128)
misc_settings = models.JSONField(null=True, blank=True)
RelatedGroup = models.ManyToManyField(Group, related_name='GroupMember')
when I created them, they appear there and seem to have everything i wanted, a username and an id/pk:
[thats the screenshot of the admin] https://i.stack.imgur.com/RqJEB.jpg)
However, when I try to check for them in the shell, they don't seem to exist properly?
In [1]: from accounts.models import User
In [2]: antoine = User(id=4)
In [3]: antoine.id
Out[3]: 4
In [4]: b = User(username="antoine")
In [5]: b.id
In [6]: b.username
Out[6]: 'antoine'
In [7]: antoine.username
Out[7]: ''
I need them to have, both a username and an id, how come they dont seem to have it?
I'm honestly pretty new to both django and databases, so I dont get why this straight forward approach doesn't work
type here
CodePudding user response:
Because your commands are just to create objects.
b = User(username="antoine")
The command above creates an User instance with username equals to 'antoine' All other values do not exist, except the ones created by default
If you want to check your objects that are stored in the Database (the ones being displayed in Django's admin page) you need to use the shell to make a query:
from accounts.models import User
obj = User.objects.get(id=4)
obj.id
obj.username